web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

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

I have the same question (0)
  • ScottDurow Profile Picture
    21 on at

    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

  • Verified answer
    Inogic Profile Picture
    703 on at

    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

  • Community Member Profile Picture
    on at

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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans