Sharepoint Connection Using Sharepoint dll in C++

NEP 21 Reputation points
2022-07-05T08:22:43.417+00:00

Dear all, before posting this question I already look for any resources that can help but can't find any (there are some related questions but they are either outdated - posted almost 10 years ago - or not exactly what I need).

My problem is I need to write a code in c++ that will connect and upload a file in a sharepoint site using sharepoint.dll. Most of my projects are written in C# where I am most comfortable and I'm new with almost no knowledge in c++.

I already have my code written and tested to work fine in C# (eg. able to authenticate and upload a file in the Sharepoint site) but the thing is I don't know how to write it in c++. I was wondering if any of you can share the simplest code you have where I can connect, authenticate and upload a file in c++?

Here's how I did it in c#:

//Connect to SP site

ClientContext ctx = new ClientContext(siteCollUrl); //siteCollUrl = my SP url  
  
     SecureString secureString = new SecureString();  
     password.ToList().ForEach(secureString.AppendChar);  
  
     ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);  
  
       Site mySite = ctx.Site;  
  
       ctx.Load(mySite);  
       ctx.ExecuteQuery();  

//Upload a file

        string libraryName = "Documents";  
        string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);  

        FileCreationInformation fcinfo = new FileCreationInformation();  
        fcinfo.Url = fileName;  
        fcinfo.Overwrite = true;  
        fcinfo.Content = System.IO.File.ReadAllBytes(filePath);  

        Web web = ctx.Web;  
        List myLibrary = web.Lists.GetByTitle(libraryName);  
        myLibrary.RootFolder.Files.Add(fcinfo);  
        ctx.ExecuteQuery();  

As you can see, almost all the methods are from the Sharepoint.dll but I can't find any example that uses the same in C++.

I'm using VS2019 with the latest framework.

TIA!

Developer technologies C++
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2022-07-07T09:28:12.417+00:00

    Hi @NEP ,

    I suggest you read this issue and follow the methods.

    And since the Sharepoint class is managed, you should use the gcnew operator instead of calling delete.

    For example:

    You need to change ClientContext ctx = new ClientContext(siteCollUrl);
    to

    Microsoft::SharePoint::Client::ClientContext ^ctx = gcnew Microsoft::SharePoint::Client::ClientContext(siteCollUrl);  
    

  2. NEP 21 Reputation points
    2022-07-07T23:40:18.403+00:00

    After spending some time I managed to rewrite my code to C++. I've created VC++ CLR project and add reference to the Sharepoint dlls.

    Connect and authenticate:

    Microsoft::SharePoint::Client::ClientContext^ ctx = gcnew Microsoft::SharePoint::Client::ClientContext(spUrl);  
    ctx->Credentials = gcnew SharePointOnlineCredentials(usrname, SecurePass);  
    Microsoft::SharePoint::Client::Site^ mySite = ctx->Site;  
      
    ctx->Load(mySite);  
    ctx->ExecuteQuery();  
    

    Upload a file:

       String^ libraryName = "Documents";  
      
        String^ fileName= filePath->Substring(filePath->LastIndexOf("\\") + 1);  
      
        FileCreationInformation^ fcinfo = gcnew FileCreationInformation();  
        fcinfo->Url = fileName;  
        fcinfo->Overwrite = true;  
        fcinfo->Content = System::IO::File::ReadAllBytes(filePath);  
       
        Microsoft::SharePoint::Client::Web^ web = ctx->Web;  
        Microsoft::SharePoint::Client::List^ myLibrary = web->Lists->GetByTitle(libraryName);  
        myLibrary->RootFolder->Files->Add(fcinfo);  
        ctx->ExecuteQuery();  
    
    0 comments No comments

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.