6 Mar’19

D365 General

Share 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

  • How to enable Document ID in SharePoint Online

    16 May’ 2019

    Introduction: As you know that SharePoint Online can be used as a document management platform and with the help of Docu...

    Read more
  • Count Number of files in SharePoint folder

    15 May’ 2019

    Introduction: This blog explains how to Count No of files in SharePoint folder using MS Flow. Steps to be followed:Sel...

    Read more
  • Professional Services Automation (PSA) for CPA firms

    14 May’ 2019

    With the evolution of Technology, every industry is getting process oriented and so are CPA firms getting more organized...

    Read more
  • What if the Resource Requirement Still appears post the task is deleted from Schedule (WBS) in PSA V3?

    13 May’ 2019

    Problem Definition: I have observed a fact in PSA that when the task in PSA Schedule (WBS) is deleted, the corresponding...

    Read more
  • Discount Settings for D365 Sales Line Items – Line Items or Per Unit?

    10 May’ 2019

    For Line Item entities Opportunity Products, Quotes Products, Order Products or Invoice Products, you can chose what typ...

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