Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

SharePoint online folder creation from CRM online plugin not working.

Posted on by Microsoft Employee

Hi All,

I have used the sample code provided on the link

code.msdn.microsoft.com/.../Discussions

to create a plugin firing synchronously on creation of account record to create a folder in the SharePoint online.

I am getting error message as below:

The remote server returned an error: (411) Length Required. By debugging the code I can see that it is failing in the method

public static byte[] SendHttpRequest(Uri uri, String method, byte[] requestContent = null, string contentType = null, HttpWebRequest clientHandler = null, Dictionary<string, string> headers = null)
{
HttpWebRequest request = clientHandler == null ? (HttpWebRequest)HttpWebRequest.Create(uri): clientHandler;

byte[] responseStream;


request.Method = method;
request.Accept = contentType;
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; // This must be here as you will receive 403 otherwise
request.AllowAutoRedirect = false; // This is key, otherwise it will redirect to failed login SP page

// append additional headers to the request
if (headers != null)
{
foreach (var header in headers)
{
if (request.Headers.AllKeys.Contains(header.Key))
{
request.Headers.Remove(header.Key);
}

request.Headers.Add(header.Key, header.Value);
}
}


if (requestContent != null && (method == "POST" || method == "PUT" || method == "DELETE"))
{
if (!string.IsNullOrEmpty(contentType))
{
request.ContentType = contentType; // if the request has a body set the MIME type
}

request.ContentLength = requestContent.Length > 0 ? requestContent.Length : 0;
using (Stream s = request.GetRequestStream())
{
s.Write(requestContent, 0, requestContent.Length);
s.Close();

}
}

// Not using Using here as you may still like to access the reponse outside of this method
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
responseStream = Encoding.UTF8.GetBytes(sr.ReadToEnd());

return responseStream;
}

The call to this method is from the method as below:

private Uri GetAdfsAuthUrl()
{
Uri corpAdfsProxyUrl = null;

Uri msoHrdUri = new Uri(msoHrdUrl);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(msoHrdUri);
// make a post request with the user's login name to
// MSO HRD (Home Realm Discovery) service so it can find
// out the url of the federation service (corporate ADFS)
// responsible for authenticating the user

byte[] response = HttpHelper.SendHttpRequest(
new Uri(msoHrdUrl),
"POST",
Encoding.UTF8.GetBytes(String.Format("handler=1&login={0}", this.username)), // pass in the login name in the body of the form
"application/x-www-form-urlencoded",
request);

STSInfo info = Deserialize<STSInfo>(response);

if (info != null && !String.IsNullOrEmpty(info.AuthURL))
{
corpAdfsProxyUrl = new Uri(info.AuthURL);
}


return corpAdfsProxyUrl;
}

The content length is definitely been mentioned and is not zero. Not sure why I am getting this error message.

Any help in this regards is much appreciated.

Many Thanks

*This post is locked for comments

  • necsa Profile Picture
    necsa 3,455 on at
    RE: SharePoint online folder creation from CRM online plugin not working.

    Can you share please your solution that will be helpfull for the another in the future.

  • Amit007 Profile Picture
    Amit007 145 on at
    RE: SharePoint online folder creation from CRM online plugin not working.

    I have resolved this and it is working fine now.

  • Amit007 Profile Picture
    Amit007 145 on at
    RE: SharePoint online folder creation from CRM online plugin not working.

    I am also using same code to create folder in SharePoint online as custom workflows which is working fine and when i am using it to upload file in share point using it as plugin then it stopped working at GetAdfsAuthUrl() but it is working when i am using the same code in console...so i am not getting why i am facing this issue.

  • Pradeep47 Profile Picture
    Pradeep47 50 on at
    RE: SharePoint online folder creation from CRM online plugin not working.

    Hi were you able to find a fix for this issue ? This is because of the conditional azure rules that is applied on the service account which is used for integration. we can avoid this by adding security group with Basic authentication this will solve the problem.

    there are two ways to resolve this issue, one is add official azure IP range to azure trusted IP list. otherwise we need to change the code to modern authentication. 

    but problem is my client doesn't want to apply basic authentication. can we change the same code to use modern authentication!! anyway other workaround..  

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: SharePoint online folder creation from CRM online plugin not working.

    Hi,

    To rule out issues with sandbox, try running the same code from a console app and see if it works. Try hardoced value for the folder path, folder name.

    Hope this helps.

  • Arun Vinoth Profile Picture
    Arun Vinoth 11,613 on at
    RE: SharePoint online folder creation from CRM online plugin not working.

    Am also blocked with this exact issue. Problem is with authentication token SAML is missing.

    I tested the same code with trial SPO tenant, no problem at all. Even if I replay the plugin using plugin profiling log in PRT it succeeded.

    Probably due to some sandbox restrictions & am working with MS support to fix my issue along with my AD team.

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans