Forum Discussion

Avi65's avatar
Avi65
Brass Contributor
Dec 15, 2023

How to add sharePoint item and attachment in SharePoint Cloud from dot net application?

Hello 

We have a one .Net application using C# with backend as SQL Server on SharePoint 2016 framework. SP2016 using for access management purpose. Whenever new item is created or new attachment is uploaded in existing item or with new item, it should also upload in SharePoint Online List/Library. 

 

How this can be implemented ?

 

Regards

Avian

  • BarryGoblon's avatar
    BarryGoblon
    Iron Contributor

    Avi65  To add SharePoint items and attachments to SharePoint Online from a .NET application, you can use the SharePoint Online REST API. Here's a step-by-step guide on how to do this:

    Authenticate to SharePoint Online: Start by authenticating to SharePoint Online using the OAuth 2.0 authorization code flow. This will allow your .NET application to access SharePoint Online resources on behalf of the user.

     

    Create a new SharePoint item: To create a new SharePoint item, you will need to make a POST request to the SharePoint Online API endpoint for the list where you want to create the item. The request body should contain the item's properties, such as title, description, and any other relevant columns.

    Upload an attachment: To upload an attachment to an existing SharePoint item, you will need to make a PUT request to the SharePoint Online API endpoint for the item. The request body should contain the attachment's data, which can be represented as a byte array or as a Base64-encoded string.

     

    Upload an attachment with a new item: To upload an attachment with a new SharePoint item, you can follow the same process as for uploading an attachment to an existing item, but you will need to make a POST request instead of a PUT request.

     

    Here's an example of how to create a new SharePoint item and upload an attachment using the SharePoint Online REST API in C#:

     


    using System;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text.Json;
    using System.IO;

    public class SharePointItemUploader
    {
    public static void UploadItemWithAttachment(string sharepointUrl, string listitemName, string attachmentFilePath)
    {
    // Create an HTTP client
    var httpClient = new HttpClient();

    // Authenticate to SharePoint Online
    var token = GetAccessToken(sharepointUrl);
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

    // Create a new item
    var itemData = "{ \"Title\": \"My New Item\", \"Description\": \"This is a new item with an attachment.\" }";
    var itemContent = Encoding.UTF8.GetBytes(itemData);

    var itemRequest = new HttpRequestMessage(HttpMethod.Post, $"https://{sharepointUrl}/sites/{sharepointSite}/Lists/{sharepointList}/Items");
    itemRequest.Content = new StringContent(itemData, Encoding.UTF8, "application/json");

    var response = httpClient.SendAsync(itemRequest).Result;
    if (response.IsSuccessStatusCode)
    {
    Console.WriteLine("Item created successfully.");

    // Upload an attachment
    var attachmentContent = File.ReadAllBytes(attachmentFilePath);
    var attachmentRequest = new HttpRequestMessage(HttpMethod.Put, $"https://{sharepointUrl}/sites/{sharepointSite}/Lists/{sharepointList}/Items/{itemId}/Attachments/{attachmentName}");
    attachmentRequest.Content = new StringContent(attachmentContent, Encoding.UTF8, "application/octet-stream");

    var attachmentResponse = httpClient.SendAsync(attachmentRequest).Result;
    if (attachmentResponse.IsSuccessStatusCode)
    {
    Console.WriteLine("Attachment uploaded successfully.");
    }
    else
    {
    Console.WriteLine("Failed to upload attachment: " + attachmentResponse.StatusCode);
    }
    }
    else
    {
    Console.WriteLine("Failed to create item: " + response.StatusCode);
    }
    }

    private static string GetAccessToken(string sharepointUrl)
    {
    // Implement your OAuth 2.0 authorization code flow code here
    // ...

    // Return the access token
    return "YOUR_ACCESS_TOKEN";
    }
    }

Resources