Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Upload documents to SharePoint in Plugin

Posted on by 1,445

Hi,

In my project we are integrating CRMOnline to SharePoint using server side integration. All works fine. Now there is requirement to upload the document from plugin to SharePoint. 

I am able to find sdk sample codes for creating SharePointSites and Locations.

https://msdn.microsoft.com/en-us/library/gg309548.aspx

But I am not able to find a sample code for uploading documents to the SharePoint Sites. Is there any message to do so or not yet available.

If available can anyone please provide me a sample code.

If not available can any one suggest any work around. I have found the below work around using rest. But is there any other way of doing this?

https://mscrmsama.wordpress.com/2016/03/21/plugin-to-upload-file-in-sharepoint-integrated-with-crm-2016/

Regards,

Pramod

*This post is locked for comments

  • Upload documents to SharePoint in Plugin
    Hello, 
     
    I am looking for the same, I have tried the code from few blogs but nothing is working. Can someone help me with this. I am getting 403 Forbidden error with the code I am trying. 
     
    Here is the API call 

    URL = "http://DOMAIN.sharepoint.com/sites/MyPowerPlatformLearnings/_api/web/GetFolderByServerRelativeUrl('SharedDocuments/Datavesrse')/Files/add(url='a.txt',overwrite=true)";

  • Pramod M Profile Picture
    Pramod M 1,445 on at
    RE: Upload documents to SharePoint in Plugin

    Hi,

    I was using the below code to create and upload document. The authorization isssue may be that the user don't have appropriate permission on sharepoint server.

    public void UploadDocument(string siteUrl, string relativePath, byte[] documentBytes, string fileName)

           {

               if (siteUrl != _siteUrl)

               {

                   _siteUrl = siteUrl;

                   Uri spSite = new Uri(siteUrl);

                   _spo = SpoAuthUtility.Create(spSite, _username, WebUtility.HtmlEncode(_password), false);

               }

               string resourceUrl = siteUrl + "/_api/web/GetFolderByServerRelativeUrl('" + relativePath + "')/Files/add(url = '" + fileName + "',overwrite=true)";

               //throw new InvalidWorkflowException("resourceUrl:::" + resourceUrl);

               Uri url = new Uri(resourceUrl);

               string digest = _spo.GetRequestDigest();

               // Set X-RequestDigest

               var webRequest = (HttpWebRequest)HttpWebRequest.Create(url);

               webRequest.Headers.Add("X-RequestDigest", digest);

               // Send a json odata request to SPO rest services to fetch all list items for the list.

               byte[] result = HttpHelper.SendODataJsonRequest(

                 url,

                 "POST", // reading data from SP through the rest api usually uses the GET verb

                 documentBytes,

                 webRequest,

                 _spo // pass in the helper object that allows us to make authenticated calls to SPO rest services

                 );

               string response = Encoding.UTF8.GetString(result, 0, result.Length);

           }

    and the utility from scott's post

    code.msdn.microsoft.com/.../sourcecode;pathId=1614921635

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Upload documents to SharePoint in Plugin

    Hi Pramod,

    {

       "error": {

           "code": "-2147024891, System.UnauthorizedAccessException",

           "message": {

               "lang": "en-US",

               "value": "Access denied. You do not have permission to perform this action or access this resource."

           }

       }

    }

    i am getting 403 error while uploading the file using REST API and Scott's code. Could you please help me in to this ?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Upload documents to SharePoint in Plugin

    Hi,

    I am getting below error, 

    Exception Message: Exception has been thrown by the target of an invocation.

    Stack Trace : at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)

    at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
    at System.Delegate.DynamicInvokeImpl(Object[] args)
    at Microsoft.Xrm.Sdk.Linq.QueryProvider.DynamicInvoke(Delegate project, Object[] args)
    at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateExpressionToConditionValue(Expression exp, ParameterExpression[] parameters)
    at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhereCondition(BinaryExpression be, FilterExpressionWrapper parentFilter, Func`2 getFilter, Func`2 getLinkLookup, Boolean negate)
    at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhere(String parameterName, BinaryExpression be, FilterExpressionWrapper parentFilter, Func`2 getFilter, List`1 linkLookups, Boolean negate)
    at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhereBoolean(String parameterName, Expression exp, FilterExpressionWrapper parentFilter, Func`2 getFilter, List`1 linkLookups, BinaryExpression parent, Boolean negate)
    at Microsoft.Xrm.Sdk.Linq.QueryProvider.TranslateWhere(QueryExpression qe, String parameterName, Expression exp, List`1 linkLookups)
    at Microsoft.Xrm.Sdk.Linq.QueryProvider.GetQueryExpression(Expression expression, Boolean& throwIfSequenceIsEmpty, Boolean& throwIfSequenceNotSingle, Projection& projection, NavigationSource& source, List`1& linkLookups)
    at Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute[TElement](Expression expression)
    at Microsoft.Xrm.Sdk.Linq.QueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
    at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
    at CRMPluginProject1._365Immo.Laurus.SharePoint.DocumentLocationHelper.GetDocumentLocationPath(SharePointDocumentLocation parentLocation)
    at CRMPluginProject1._365Immo.Laurus.SharePoint.DocumentLocationHelper.CreateDocumentLocation(Entity site, String documentLibraryName, EntityReference regardingRecord)
    at CRMPluginProject1._365Immo.Laurus.Plugins.GetSharePointFile.Execute(IServiceProvider serviceProvider)

    Inner Exception : The demand failed due to the code access security information captured during the creation of an anonymously hosted dynamic method. In order for this operation to succeed, ensure that the demand would have succeeded at the time the method was created. See go.microsoft.com/fwlink for more information.

    Stack Trace : at lambda_method(Closure )

    Please help me to make it working.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Upload documents to SharePoint in Plugin

    Hi Pramod,

    I have same type of requirement but, I am not able to make it working. please help me to solve it.

    I have used power attachment add-on to upload document in share point and its working properly and returning file URL(e.g. "xyz.sharepoint.com/.../File01_07262017.pdf"), But now i have requirement to get file stream from maintained URL, As i have to attach this file to email annotation.

    I have also try to get it via share point client but no luck. Please check post community.dynamics.com/.../247000

    Please help me to solve it.

    Thanks,

    Krutesh Amin

  • Verified answer
    Pramod M Profile Picture
    Pramod M 1,445 on at
    RE: Upload documents to SharePoint in Plugin

    Hi Inlogic,

    This is really helpful. Thank you.

    Before your post I saw Scott's post and some below posts which helped me to upload the document.

    develop1.net/.../SharePoint-Integration-Reloaded-e28093-Part-3.aspx

    code.msdn.microsoft.com/SharePoint-Integration-c5f21604

    bingsoft.wordpress.com/.../crm-online-to-sharepoint-online-integration-using-rest-and-adfs

    mscrmsama.wordpress.com/.../plugin-to-upload-file-in-sharepoint-integrated-with-crm-2016

    Regards,

    Pramod

  • Verified answer
    Inogic Profile Picture
    Inogic 24,094 on at
    RE: Upload documents to SharePoint in Plugin
    Hi Pramod,
     
    Yes you can upload files from CRM online to SharePoint online using Plugin.
     
    You may refer below blog for more information:
     
     
    Hope this helps!
     
    Thanks!
  • Pramod M Profile Picture
    Pramod M 1,445 on at
    RE: Upload documents to SharePoint in Plugin

    This is using SharePoint Libraries, in plugin online we cannot merge these dlls.

  • NicksTuk Profile Picture
    NicksTuk 1,833 on at
    RE: Upload documents to SharePoint in Plugin

    An overly simplified example on uploading documents to SharePoint is here:

    www.c-sharpcorner.com/.../programmatically-adding-document-to-sharepoint-document-library

    You should be able to easily integrate that into a plugin if required.

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans