Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Adding a sharepointdocumentlocation programmatically.

(0) ShareShare
ReportReport
Posted on by

Hello, I am having some trouble getting a sharepointdocumentlocation entity on CRM to target the correct SharePoint location using relative url.  See my code below.

When I run the code and make my way to the parent entity on CRM, I can see the sharepointdocumentlocation entity has been applied with the name "TestFolder2".  But when it tries to load up the SharePoint location, it says it can't find the folder "f1ec0efaf82ce61180cb0050568469a8" and pops up a wizard to create a folder.  But before I execute the code block below, I also call a method to create the folder in the SharePoint library so I know it is there.

Any suggestions on what I may be overlooking?

ClientCredentials Creds = new ClientCredentials();
Creds.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
Uri SvcUri = new Uri("https://<crm domain>/XRMServices/2011/Organization.svc");

if (SvcUri != null && SvcUri.AbsoluteUri.Contains("https"))
    System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; };

using (OrganizationServiceProxy proxy = new OrganizationServiceProxy(SvcUri, null, Creds, null))
{
    proxy.CallerId = Guid.Parse("1ef8f22b-c6c9-e511-80ca-0050568469a8");

    Entity sharepointdocumentlocation = new Entity("sharepointdocumentlocation");

    string FetchXML = @"
        <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true' >
            <entity name='sharepointdocumentlocation' >
                <attribute name='sharepointdocumentlocationid' />
                <filter type='and' >
                    <condition attribute='relativeurl' operator='eq' value='lead' />
                </filter>
            </entity>
        </fetch>";

    EntityCollection result = proxy.RetrieveMultiple(new FetchExpression(FetchXML));
    Console.WriteLine(result[0].Id);
    sharepointdocumentlocation["name"] = "TestFolder2";
    sharepointdocumentlocation["parentsiteorlocation"] = result[0].Id;
    sharepointdocumentlocation["relativeurl"] = "f1ec0efaf82ce61180cb0050568469a8";
    sharepointdocumentlocation["regardingobjectid"] = new EntityReference("lead", Guid.Parse("f1ec0efaf82ce61180cb0050568469a8"));
    Guid sharepointdocumentlocationid = proxy.Create(sharepointdocumentlocation);
    Console.WriteLine(sharepointdocumentlocationid);

    Console.ReadKey();
}

*This post is locked for comments

  • Community Member Profile Picture
    on at
    RE: Adding a sharepointdocumentlocation programmatically.

    That was the problem.  Made your correction to my code.  Now it works.  Thank you for looking over my code with a keen eye.

  • Verified answer
    Inogic Profile Picture
    529 on at
    RE: Adding a sharepointdocumentlocation programmatically.

    Hi Matthew,

    There seems to be an error in your posted code. See the below highlighted line:

    sharepointdocumentlocation["parentsiteorlocation"] = result[0].Id;

    It should be an EntityReference value and not a GUID. You can change it to below line and try:

    sharepointdocumentlocation["parentsiteorlocation"] = result[0].ToEntityReference();

    Hope this helps to resolve the issue.

    Thanks,
    Sam

  • ScottDurow Profile Picture
    19 on at
    RE: Adding a sharepointdocumentlocation programmatically.

    Hi,

    So you have created a folder in SharePoint that is in the document library 'lead' and the folder name is 'f1ec0efaf82ce61180cb0050568469a8' - is that right?

    Does the user who is authenticated with CRM/Sharepoint have access to the folder?

    Do you have more than one document location with the relativeurl set to lead?

    What happens if you manually create a document locaton with the folder name - does it load - if so, compare the document location you created manually with that is created by your code.

    Hope this helps

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
dkrishna Profile Picture

dkrishna 6

Featured topics

Product updates

Dynamics 365 release plans