web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(1) ShareShare
ReportReport
Posted on by 1,829

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

I have the same question (0)
  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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.

  • Community Member Profile Picture
    on at

    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 ?

  • Verified answer
    Hemant Kumar Sahu Profile Picture
    1,829 on at

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

  • Community Member Profile Picture
    on at

    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 ?

  • Hemant Kumar Sahu Profile Picture
    1,829 on at

    Hi Ravi,

    Did u solve the issue?

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

    Thanks

    Hemant

  • ayush kar Profile Picture
    2,654 on at

    Hi Hemant,

    Could you please share your code.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans