sharepoint CSOM code example using visual studio code editor

sweetu ve 286 Reputation points
2021-05-03T09:23:24.397+00:00

I am using visual studio code editor and i need some references or steps how to run sample csom sharepoint office 365 lists related stuffs for the same. Also i have installed visual studio code version 1.5.5.0 not sure how to reference sharepoint related dlls or how to run basic CSOM related sharepoint list items .Appreciate kind help in this regards

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,126 Reputation points
    2021-05-04T06:17:54.603+00:00

    First, create a console app : dotnet new console . Refer to this article for more: https://learn.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code

    Then add the pnp framework package: dotnet add package PnP.Framework --version 1.4.0

    93476-image.png

    The code would be like this:

    using System;  
    using Microsoft.SharePoint.Client;  
    using PnP.Framework;  
      
    namespace CSOM  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                string site = "https://tenant.sharepoint.com/sites/test";  
                string appId = "<app clientID>";  
                string appSecret = "<app secret>";  
                var authMgr=new AuthenticationManager();  
                var ctx=authMgr.GetACSAppOnlyContext(site,appId,appSecret);  
                var web = ctx.Web;  
                ctx.Load(web);  
                ctx.ExecuteQuery();  
                      
                //copy list here  
            }  
        }  
    }  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. MichaelHan-MSFT 18,126 Reputation points
    2021-05-04T02:48:08.89+00:00

    Hi @sweetu ve ,

    VS Code supports limited for .Net Framework projects, you could refer to the below articles:

    So if you are creating .Net Framework projects, I would suggest you use Visual Sudio instead of vs code.

    If you are using CSOM for .Net Core, you could create a demo .NET console application referring to here: https://learn.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code

    To add SharePoint related assembly references in Visual Studio Code, use this commned dotnet add package. More details are in this question: How do I add assembly references in Visual Studio Code?

    Besides, in .Net Core, you need to use modern authentication with CSOM, which means the approach using user/password based authentication via the SharePointOnlineCredentials Class would not work. Please read this article for more: Using CSOM for .NET Standard instead of CSOM for .NET Framework


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.


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.