Friday, April 26, 2013

Bulk Upload Documents to Online SharePoint from MS CRM Online



Microsoft Dynamics CRM 2011 allows managing documents available in SharePoint with native methods. This allows create, upload, view and delete documents available in SharePoint from within Microsoft Dynamics CRM.
Bulk upload of attachment to any MS CRM using on premise SharePoint can be achieved using native web service. But this gets tricky between Online SharePoint and Online MS CRM. Below are steps for achieving this using CRM plugin.
  • Gain access to Online SharePoint service. Please use this link to get detailed information to access SharePoint service
  • Retrieve MS CRM attachments document body.
  • Upload the retrieved attachments to the SP online using SP service retrieved in first step.

Note:   This above web service needs to be publically hosted. This hosted web service is then consumed through Plugin registered in MSCRM online.

Here is the part of web service code and for uploading attachments to SP online.

string FinalURL = <<SHAREPOINT URL/FILENAME + EXTENSTION>>
                HttpWebRequest request = HttpWebRequest.Create(FinalURL) as HttpWebRequest;
                request.Method = "PUT";
                request.ContentType = "application/x-www-form-urlencoded";
                request.CookieContainer = new CookieContainer();
                request.CookieContainer.Add(SetAuthCookies(<<SHAREPOINT URL>>, <<USERNAME>>, <<PASSWORD>>)); // Online SP Service
                request.AllowAutoRedirect = false;
                request.UserAgent = userAgent;
                request.Headers.Add("Overwrite""F");
                request.Timeout = Timeout.Infinite;

                Byte[] Documentstream = Convert.FromBase64String(<<DOCUMENTSBODY>>);
                Stream stream = new MemoryStream(Documentstream);

                Stream outStream = request.GetRequestStream();
                string status = CopyStream(stream, outStream);

                if (status == "success")
                {
                    outStream.Close();
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                }

Hope this is helpful!!! Happy Coding!!!

No comments:

Post a Comment