Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Upload files from CRM online to SharePoint online using Plugin(Error: forbidden url 403,using REST API)

Posted on by 1,825

Hi Expert,

I am trying to upload file to SharePoint using Plugin.
As I am uisng Dynamics V9 online so can not use the ("“Microsoft.SharePoint.Client.dll” and “Microsoft.SharePoint.Client.Runtime.dll”"). It gives System.Security.Permissions.EnvironmentPermission error and therefore this method probably does not support CRM Online.

So I trying do this with the help of REST

I followed the below url
https://mscrmsama.wordpress.com/2016/03/18/upload-a-file-to-sharepoint-2013-programmatically-externally/
But "getting forbidden url 403" at line
HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();

private static string GetFormDigestValue(string siteurl, NetworkCredential credentials)
{
string newFormDigest = “”;
HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(siteurl + “/_api/contextinfo”);
endpointRequest.Method = “POST”;
endpointRequest.ContentLength = 0;
endpointRequest.Credentials = credentials;
endpointRequest.Accept = “application/json;odata=verbose”;

try
{
HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

try
{

WebResponse webResp = endpointRequest.GetResponse();
Stream webStream = webResp.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
string response = responseReader.ReadToEnd();
var j = JObject.Parse(response);
var jObj = (JObject)JsonConvert.DeserializeObject(response);
foreach (var item in jObj[“d”].Children())
{
newFormDigest = item.First()[“FormDigestValue”].ToString();
}
responseReader.Close();

}
catch (Exception ex)
{

Console.WriteLine(ex.Message);
}

return newFormDigest;
}


Thanks In Advanced

*This post is locked for comments

  • ayush kar Profile Picture
    ayush kar 2,654 on at
    RE: Upload files from CRM online to SharePoint online using Plugin(Error: forbidden url 403,using REST API)

    Hi Hemant,

    Could you please share your code.

  • Hemant Kumar Sahu Profile Picture
    Hemant Kumar Sahu 1,825 on at
    RE: Upload files from CRM online to SharePoint online using Plugin(Error: forbidden url 403,using REST API)

    Hi Ravi,

    Did u solve the issue?

    If you make overwrite false, make sure the complete file url should not be duplicate.

    Thanks

    Hemant

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Upload files from CRM online to SharePoint online using Plugin(Error: forbidden url 403,using REST API)

    Thanks a lot Hemant ! That helped.

    However, i am getting 400- Bad request error while setting overwrite=false in upload request.

     string restQuery = "_api/web/GetFolderByServerRelativeUrl('/" + libraryName + "')/Files/add(url='" + fileName + "',overwrite=false)";

    It is working fine if i set overwrite= true.

    Any idea how can i disable auto overwriting option in sharepoint ?

  • Verified answer
    Hemant Kumar Sahu Profile Picture
    Hemant Kumar Sahu 1,825 on at
    RE: Upload files from CRM online to SharePoint online using Plugin(Error: forbidden url 403,using REST API)

    I followed this, This helped me code.msdn.microsoft.com/SharePoint-Integration-c5f21604.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Upload files from CRM online to SharePoint online using Plugin(Error: forbidden url 403,using REST API)

    Hi Ravi,

    {

       "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 ?

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Upload files from CRM online to SharePoint online using Plugin(Error: forbidden url 403,using REST API)

    Hi Hemant,

    This looks like the authentication/ connction issue. Instead of writing the plugin to upload file to SharePoint directly, I would suggest you to first just try and confirm connecting your CRM Online to SharePoint Online and once that done you can try the upload file.  

    You could download the code sample from here and then keep enhancing it as per your needs.

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

    community.dynamics.com/.../creating-sharepoint-folder-automatically-custom-plugin-code

    Hope this helps.

  • Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Upload files from CRM online to SharePoint online using Plugin(Error: forbidden url 403,using REST API)

    Take a look at the following response.

    It has several links besides the one you were using to do this using rest services. Your code seems to be based on one of the links in this forum post.

    community.dynamics.com/.../205584

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