Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Programmatically adding a site collection to crm

Posted on by 5,241

When you run through the wizard of Document Management settings, you check the entities which you want to enable documents on, and you enter the url to the SharePoint site collection:

2016_2D00_11_2D00_27-21_5F00_26_5F00_48_2D00_Document-Management-Settings.png

When I do this, everything works fine. A SharePoint site is added in the SharePointSite entity in CRM. However I want to accomplish the same with coding. So I deleted the SharePoint site in CRM and tried to recreate it programmatically.

However I don't seem to get this to work. Either I get an error that there are no active site when I only create the site in CRM. Or I get other errors, e.g. Invalid User Authorization:

2016_2D00_11_2D00_27-21_5F00_32_5F00_49_2D00_Account_5F00_-Adventure-Works.png

That error is for the exact same site I create programmatically just after I deleted that exact site created with the wizard.

I've been through the documentation:https://msdn.microsoft.com/en-us/library/gg327818.aspx and I can't seem to find anywhere that what I'm trying to do is unsupported.

Am I missing something? I assume that creating a site record in CRM is not enough and it needs to be validated. Can I do this programmatically as wel?

What I'm trying to do is to create a document site for every account in CRM. As there will be over 20.000 accounts and growing fast, I don't want to use the default integration and I don't want to manually create every Site Colllection in CRM as SharePoint site.

Btw, I'm using CRM online and SharePoint online.

*This post is locked for comments

  • Verified answer
    M.T. Eikelenboom Profile Picture
    M.T. Eikelenboom 5,241 on at
    RE: Programmatically adding a site collection to crm

    I tried different options en come to the following conclusion:

    1. Adding a new site collection (not a subsite) to CRM will result in the Invalid User Authorization. When you configure the same site through the interface in CRM you overcome this error.

    2. Adding a subsite of that site collection to CRM has to be done on the CRM sharepointsite entity, as a child site of the site collection with a relative url.

    3. Adding a library is done by adding a CRM documentlocation with a relative url and the parent site of no 2 in this list.

    Too bad that I can't add a new site collection. There's probably some authentication process in the background.

  • M.T. Eikelenboom Profile Picture
    M.T. Eikelenboom 5,241 on at
    RE: Programmatically adding a site collection to crm

    I've tested it again. To be sure I configured SharePoint server based integration on https://tenant.sharepoint.com/sites/crm through the wizard and successfully tested it on an account. Then I run this code to create a new site collection and document locations:

    var site = new SharePointSite();
    site.Name = "Custom site collection";
    site.Description = "Default site";
    site.AbsoluteURL = "https://dncps.sharepoint.com/sites/a216";
    var siteRef = xrm.Create(site);
    
    var mainDocLoc = new SharePointDocumentLocation();
    mainDocLoc.Name = "Main document location (subsite)";
    mainDocLoc.RelativeUrl = "ABCO9M33";
    mainDocLoc.ParentSiteOrLocation = new CrmEntityReference("sharepointsite", siteRef);
    var mainDocLocId = xrm.Create(mainDocLoc);
    
    var docloc = new SharePointDocumentLocation();
    docloc.Name = "Shared Documents";
    docloc.RelativeUrl = "Shared Documents";
    docloc.RegardingObjectId = new CrmEntityReference("account", new Guid("BCA19CDD-88DF-E311-B8E5-6C3BE5A8B200"));
    docloc.ParentSiteOrLocation = new CrmEntityReference("sharepointdocumentlocation", mainDocLocId);
    var doclibid = xrm.Create(docloc);
    
    RetrieveAbsoluteAndSiteCollectionUrlRequest retrieveRequest = new RetrieveAbsoluteAndSiteCollectionUrlRequest
    {
        Target = new CrmEntityReference(SharePointDocumentLocation.EntityLogicalName, doclibid)
    };
    RetrieveAbsoluteAndSiteCollectionUrlResponse retriveResponse = (RetrieveAbsoluteAndSiteCollectionUrlResponse)xrm.Execute(retrieveRequest);
    
    Console.WriteLine("Absolute URL of document location record is '{0}'.", retriveResponse.AbsoluteUrl.ToString());
    
    return doclibid;



    On the console it writes the url, I copy paste that in the browser to test if the location is working, and the document library in SharePoint successfully loads.

    Then I go back to CRM, open the account and the documents pane, I get the error:

    Invalid User Authorization


    Just to test, I run the wizard in CRM again for the url above: https://tenant.sharepoint.com/sites/a216

    Then, when I go back to the documents pane in CRM it gives the following error:

    A list of files from the following document locations could not be retrieved: tenant.sharepoint.com/.../a216


    When I click Open Location from the grid menu bar, it opens SharePoint without any errors.

    I'm confused why this isn't working.

    1. Why do I get Invalid User Authorization and it disappears when I run the wizard on the same location?
    2. Why can't it find documents when I have verified that the url exists and CRM is able to navigate to the location?
  • M.T. Eikelenboom Profile Picture
    M.T. Eikelenboom 5,241 on at
    RE: Programmatically adding a site collection to crm

    Thanks, I'll give it a try again, and if it's not working I'll post the code.

  • ScottDurow Profile Picture
    ScottDurow 50,177 on at
    RE: Programmatically adding a site collection to crm

    CRM doesn't do anything to SharePoint other than ensure that the document libraries and folders exist - and then it marks the sharepoint site as 'validated' as a simple boolean flag.

    As long as your site contains the locations - and you create those locations in CRM then everything should work. I've done this many times before.

  • M.T. Eikelenboom Profile Picture
    M.T. Eikelenboom 5,241 on at
    RE: Programmatically adding a site collection to crm

    Scott,

    Thanks for the answer, but creating the SharePoint artifacts is not what I'm talking about. What I'm doing is creating a SharePoint site with certain lists and libraries using an Azure Function. When that's done, I want to write that location back to CRM to use the out of the box features for the integration.

    Writing that record in CRM doesn't seem to work as I get errors, when I try to open the documents pane in CRM. And it specifically seems to be around adding a new site collection to CRM. E.g. when you create a site collection through the wizard it has the status validated. I'm wondering what CRM does to validate the site and if I could run that validation programmatically.

  • Suggested answer
    ScottDurow Profile Picture
    ScottDurow 50,177 on at
    RE: Programmatically adding a site collection to crm

    Hi,

    You are right - creating the site in CRM doesn't create the site in share point.

    I did a blog post series with sample code on how to do this - community.dynamics.com/.../sharepoint-integration-reloaded-part-2

    Hope this answers your question.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans