6 Mar’19

D365 GeneralShare SharePoint document programmatically

Introduction:

Sometimes it is required to share the certain documents with outside users. It is possible that user can share that document as hard copy. If that document is hosted on SharePoint user can make a shareable link.

Description:

Sharing a document to the outside users are possible via directly going to SharePoint and creating a shareable link. But what if you want to generate a shareable link using the C# code.

You can follow below code to generate shareable link in Online SharePoint.

1. Get the link
You need to pass the document link to below function as shown.

currentLink = https://organization.sharepoint.com/cf_car/JTN_89330b3c-beb5-e811-a968-000d3a324f4c/Untitled2.png

2. Get the base url of your SharePoint and generate the service context

using (var context = new ClientContext(Model.SharePointBaseURL)){}

3. Authenticate the user

foreach (var c in Model.AdminPassword) passWord.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials(Model.AdminUserName, passWord);

4. CreateAnonymousLink method which will generate the shareable link

var orgEditLink = Web.CreateAnonymousLink(context, currentLink, true);

5. Execute the query

context.ExecuteQuery();

6. Finally get the public link

editUrl = orgEditLink.Value;

Complete code:

        private static string UpdateLinkTOShareable(string currentLink, Entity document)
        {
            string editUrl = string.Empty;

            using (var context = new ClientContext(Model.SharePointBaseURL))
            {
                var passWord = new SecureString();
                foreach (var c in Model.AdminPassword) passWord.AppendChar(c);
                context.Credentials = new SharePointOnlineCredentials(Model.AdminUserName, passWord);
                try
                {
                    var orgEditLink = Web.CreateAnonymousLink(context, currentLink, true);
                    context.ExecuteQuery();
                    editUrl = orgEditLink.Value;

                }
                catch (Exception ex)
                {

                }
                /*Code to make link Public End*/


                return editUrl;
            }
        }
 
Written by

Subhash Mahato

Technical Consultant

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • Expenses in PSA for Fixed Bid and T&M contracts

    23 August’ 2019

    Problem Definition: One of my clients decided to use Expense module in PSA and to his surprise he could see the amount v...

    Read more
  • Setup Dockers

    23 August’ 2019

    Docker is an independent container platform that enables organizations to seamlessly build, share and run any applicatio...

    Read more
  • Qualify Lead in D365 CE: Select which entity records should be created

    20 August’ 2019

    Wave 2019 Updates brought some really sought-after features. Another one which I would like to bring up is the Lead Qual...

    Read more
  • Early Opt-in for October 2019 Wave 2 Updates

    20 August’ 2019

    August 2, 2019 was when the Wave 2 Updates were to be out. And here we are! By now, you should have gotten the option to...

    Read more
  • Setting Unified Interface App Icons

    20 August’ 2019

    Default App Icons for typical D365 environment look like these.But here’s what you can do to change these to put...

    Read more
  • We respect your privacy.
  • This field is for validation purposes and should be left unchanged.