Many of our customers annoyed and frustrated with popup that appears when they click on documents navigation link on a record to add documents. So I thought it can be easily addressed  using workflow/plugin on create of record by automatically adding the SharePoint location record.But that did not resolve problem still users prompted with popup. When  i googled on this topic i found one post from Scott but that requires to have user name and password of the sharepoint admin.

http://develop1.net/public/post/SharePoint-Integration-Reloaded-e28093-Part-3.aspx

After lot of trial and error just started with my best friend fiddler to capture request and response that were posted on click of the popup window.  CRM online uses CRM API message “CreateFolder”  to create sharepoint folder and location record. I tried to use this “CreateFolder” message in my custom workflow step to create folder but received error message “This requested can’t be executed in sandbox mode”  then tried with javascript to post same message and it worked fine.

Here is complete script to auto create sharepoint folder for CRM online. Add below script to your script resource file and save  and publish it.  Then call function createDocFolder on onload of the page that will automatically create sharepoint and location folder.

//Function to be called on onload of the page.
var myFormType = null;
function createDocFolder() {
        Xrm.Page.data.entity.addOnSave(function () {
            myFormType = Xrm.Page.ui.getFormType();
     });
       if (myFormType === 1 && Xrm.Page.ui.getFormType() !== 1) { // calls this method only when new record is saved.
           var clientUrl = Xrm.Page.context.getClientUrl();
           var xmlhttp = new XMLHttpRequest();
           xmlhttp.open(“POST”, clientUrl + “/XRMServices/2011/Organization.svc/web”, false);
           xmlhttp.setRequestHeader(“Accept”, “application/xml, text/xml, */*”);
           xmlhttp.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
           xmlhttp.setRequestHeader(“SOAPAction”, “http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute”);
           xmlhttp.onreadystatechange = function () {
         if (xmlhttp.readyState == 4 && xmlhttp.status != 200) {
                      alert( xmlhttp.responseText);
        }
     };
     xmlhttp.send(buildCreateReq());
     }
}

//Function to build request

function buildCreateReq() {
var recordId = Xrm.Page.data.entity.getId().replace(“{“, “”).replace(“}”, “”);
return “<s:Envelope xmlns:s=\”http://schemas.xmlsoap.org/soap/envelope/\”>” +
” <s:Header>” +
” <SdkClientVersion xmlns=\”http://schemas.microsoft.com/xrm/2011/Contracts\”></SdkClientVersion>” +
” </s:Header>” +
” <s:Body>” +
“<Execute xmlns=\”http://schemas.microsoft.com/xrm/2011/Contracts/Services\” xmlns:i=\”http://www.w3.org/2001/XMLSchema-instance\”>” +
” <request i:type=\”b:CreateFolderRequest\” xmlns:a=\”http://schemas.microsoft.com/xrm/2011/Contracts\” xmlns:b=\”http://schemas.microsoft.com/crm/2011/Contracts\”>” +
” <a:Parameters xmlns:b=\”http://schemas.datacontract.org/2004/07/System.Collections.Generic\”>” +
” <a:KeyValuePairOfstringanyType>” +
” <b:key>FolderName</b:key>” +
” <b:value i:type=\”c:string\” xmlns:c=\”http://www.w3.org/2001/XMLSchema\”>” + Xrm.Page.data.entity.getPrimaryAttributeValue() + “_” + recordId + “</b:value>” +
” </a:KeyValuePairOfstringanyType>” +
” <a:KeyValuePairOfstringanyType>” +
” <b:key>RegardingObjectType</b:key>” +
” <b:value i:type=\”c:int\” xmlns:c=\”http://www.w3.org/2001/XMLSchema\”>” + Xrm.Page.context.getQueryStringParameters().etc + “</b:value>” +
” </a:KeyValuePairOfstringanyType>” +
” <a:KeyValuePairOfstringanyType>” +
” <b:key>RegardingObjectId</b:key>” +
” <b:value i:type=\”c:string\” xmlns:c=\”http://www.w3.org/2001/XMLSchema\”>” + recordId + “</b:value>” +
” </a:KeyValuePairOfstringanyType>” +
” <a:KeyValuePairOfstringanyType>” +
” <b:key>DocumentType</b:key>” +
” <b:value i:nil=\”true\” />” +
” </a:KeyValuePairOfstringanyType>” +
” <a:KeyValuePairOfstringanyType>” +
” <b:key>ParentLocationId</b:key>” +
” <b:value i:nil=\”true\” />” +
” </a:KeyValuePairOfstringanyType>” +
” </a:Parameters>” +
” <a:RequestId i:nil=\”true\” />” +
” <a:RequestName>CreateFolder</a:RequestName>” +
” </request>” +
“</Execute>” +
“</s:Body>” +
“</s:Envelope>”;
}

Click here to Download script File, change the extention of the file to js from doc.

createdocumentfolder.doc