Is it possible to login to SharePoint Online in Microsoft.SharePointOnline.CSOM C# background service, using a secret key?

Dona Maria 6 Reputation points
2022-11-23T09:30:46.513+00:00

We are using SharePoint Online as a DMS in our application. Currently we have created a user for SharePoint and using his credentials we are uploading the documents to SharePoint using Microsoft.SharePointOnline.CSOM C# background service. But we need to upload documents on behalf of other users. Is it possible to do so using a secret key?

Microsoft 365 and Office | SharePoint | For business | Windows
{count} vote

2 answers

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-11-24T07:31:33.92+00:00

    Hi @Dona Maria ,

    According to my research and testing, you can connect to SharePoint Online using Client ID/ Client Secret via CSOM. First, you need to set up an app-only principal with tenant permissions, please follow the steps in this document: Granting access using SharePoint App-Only.

    Then use the following code to connect to SharePoint Online:

    string siteUrl = "https://contoso.sharepoint.com/sites/demo";  
    using (var cc = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, "[Your Client ID]", "[Your Client Secret]"))  
    {  
        cc.Load(cc.Web, p => p.Title);  
        cc.ExecuteQuery();  
        Console.WriteLine(cc.Web.Title);  
    };  
    

    If you want to upload documents to SharePoint Online, please use the following code:

    public void UploadDocumentContentStream(ClientContext ctx, string libraryName, string filePath)  
    {  
      Web web = ctx.Web;  
      
      using (FileStream fs = new FileStream(filePath, FileMode.Open))  
      {  
        FileCreationInformation flciNewFile = new FileCreationInformation();  
      
        // This is the key difference for the first case - using ContentStream property  
        flciNewFile.ContentStream = fs;  
        flciNewFile.Url = System.IO.Path.GetFileName(filePath);  
        flciNewFile.Overwrite = true;  
      
        List docs = web.Lists.GetByTitle(libraryName);  
        Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(flciNewFile);  
      
        ctx.Load(uploadFile);  
        ctx.ExecuteQuery();  
      }  
    }  
    

    Here is my test code and test result, you can refer to it:

    static void Main(string[] args)  
            {  
                string siteUrl = "https://xxxxx.sharepoint.com/sites/zella";  
                using (var cc = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "[Your Client ID]", "[Your Client Secret]"))  
                {  
                    cc.Load(cc.Web, p => p.Title);  
                    cc.ExecuteQuery();  
                    Debug.WriteLine(cc.Web.Title);  
                    Web web = cc.Web;  
      
                    string libraryName = "LibraryTest";  
                    string filePath = "C:/Temp/1124.txt";  
      
      
                    using (FileStream fs = new FileStream(filePath, FileMode.Open))  
                    {  
                        FileCreationInformation flciNewFile = new FileCreationInformation();  
      
                        // This is the key difference for the first case - using ContentStream property  
                        flciNewFile.ContentStream = fs;  
                        flciNewFile.Url = System.IO.Path.GetFileName(filePath);  
                        flciNewFile.Overwrite = true;  
      
                        List docs = web.Lists.GetByTitle(libraryName);  
                        Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(flciNewFile);  
      
                        cc.Load(uploadFile);  
                        cc.ExecuteQuery();  
                    }  
                }  
      
            }  
    

    263795-01.png

    More information for reference: Upload large files sample SharePoint Add-in

    Hope it can help you. Thanks for your understanding.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    1 person found this answer helpful.

  2. Shivaprasad Rao 5 Reputation points
    2023-01-12T02:10:41.2966667+00:00

    Hi Tong,

    I tested above code with Pnp.framework i.e. with GetACSAppOnlyContext on .net 6.

    I am able to get the web.title, which means I am having the permissions.

    However while uploading the document its giving below error. The Admin informed I have been provided full permission for this site collection. Please suggest. Could there be a way to programmatically display permission level of my client id/client secret or could it be issue with Initializing/loading of the fields of folders/files.

    User's image

    User's image

    Best Regard,

    Shiva

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.