0

I'm working to upgrade an existing on-premises SharePoint site to a SharePoint Online site. The existing site uses a lot of code (both JS and .NET code) talking to the on-premises site via JSOM, CSOM, and REST.

A .NET windows service communicates using CSOM, and I am trying to get it to work with SharePoint Online. (Initial attempt of simply changing the site Url results in 403 Forbidden errors) The code currently uses the credentials of the service account the service is running under, which is a local domain account.

  1. Does CSOM use the REST API under the covers? I've been told that the REST API will be available. If CSOM uses the REST API, I know that firewall rules, etc. should not block it. OTOH, if CSOM requires a separate set of ports to be opened, I can't guarantee the admins have opened them up and may need to rewrite the parts of the code that use CSOM to use the REST API instead.

  2. Does CSOM, by default, use the credentials the program is running under to authenticate to SharePoint Online? If not, how do I set the credentials for CSOM?

  3. What SharePoint permissions does the service account need on the site to use CSOM and/or the REST API?

1 Answer 1

0

There are 2 ways to connect with SharePoint Online using CSOM C#.

  1. Using Username and Password
  2. Using App only authentication

Both the approaches require the Microsoft.SharePointOnline.CSOM NuGet installed.

Using username and password, you can connect to SharePoint Online using the below code.

public void GenerateContext()    
{    
    string siteCollectionUrl = "https://contoso.sharepoint.com/";    
    string userName = "[email protected]";    
    string password = "******";    
    
    // Namespace: Microsoft.SharePoint.Client    
    ClientContext ctx = new ClientContext(siteCollectionUrl);   
    
    // Namespace: System.Security  
    SecureString secureString = new SecureString();     
    password.ToList().ForEach(secureString.AppendChar);    
    
    // Namespace: Microsoft.SharePoint.Client    
    ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);   
    
    // Namespace: Microsoft.SharePoint.Client    
    Site site = ctx.Site;   
    
    ctx.Load(site);    
    ctx.ExecuteQuery();    
    
    Console.WriteLine(site.Url.ToString());    
} 

Using App Only Authentication, first, you need to register the app in SharePoint, grant proper permission, and then use the below code.

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

References:

  1. Connect To SharePoint Online Site With App Only Authentication
  2. Granting access using SharePoint App-Only
  3. Using CSOM for .NET Standard instead of CSOM for .NET Framework

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.