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)

Sharepoint Integration with MS CRM 365 online

(0) ShareShare
ReportReport
Posted on by 50

HI all,

I am trying to integrate CRM with sarepoint, I am following these links,

https://code.msdn.microsoft.com/SharePoint-Integration-c5f21604/view/Discussions#content

and

http://journeyintocrm.com/archives/182

This is the code I am tring.. 

public string GetRequestDigest()
{
string digest = String.Empty;
Uri url = new Uri(String.Format("{0}/{1}", SiteUrl, contextInfoQuery));
byte[] result = HttpHelper.SendODataJsonRequest(
url,
"POST", // Retrieving the Context Info needs a POST Method
new byte[0],
(HttpWebRequest)HttpWebRequest.Create(url),
SpoAuthUtility.Current // pass in the helper object that allows us to make authenticated calls to SPO rest services
);

// use the DataContractJsonSerializer instead of the JavascriptSerializer (system.web.extension.dll cannot be used in the sandbox)
MemoryStream stream = new MemoryStream(result);
stream.Position = 0;

DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(ContextInfo));
ContextInfo ci = (ContextInfo)json.ReadObject(stream);

/// ContextInfo ci = (ContextInfo)json.Deserialize(Encoding.UTF8.GetString(result, 0, result.Length), typeof(ContextInfo));
digest = (ci != null) ? ci.FormDigestValue : String.Empty;

return digest;
}

/// <summary>
/// Helper class to handle ContextInfo JSON Deserialisation
/// </summary>
class ContextInfo
{
public Dictionary<string, Dictionary<string, object>> d { get; set; }
public String FormDigestValue
{
get
{
string value = String.Empty;

try
{
if (d != null && d.FirstOrDefault().Value != null)
{
value = Convert.ToString(d.FirstOrDefault().Value["FormDigestValue"]);
}
}
catch { }

return value;
}
}
}

I am getting error in GetRequestDigest()  Method, Error message says : 

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Type 'SharepointUpload.SpoAuthUtility+ContextInfo' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
Detail: <OrganizationServiceFault xmlns="schemas.microsoft.com/.../Contracts" xmlns:i="www.w3.org/.../XMLSchema-instance">
<ActivityId>00000000-0000-0000-0000-000000000000</ActivityId>
<ErrorCode>-2147220970</ErrorCode>
<ErrorDetails xmlns:a="schemas.datacontract.org/.../System.Collections.Generic">
<KeyValuePairOfstringanyType>
<a:key>CallStack</a:key>
<a:value i:type="b:string" xmlns:b="www.w3.org/.../XMLSchema"> at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.get_RootContract()
at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalIsStartObject(XmlReaderDelegator reader)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader reader)
at SharepointUpload.SpoAuthUtility.GetRequestDigest()
at SharepointUpload.UploadFile.Execute(IServiceProvider serviceProvider)
at PluginProfiler.Library.PluginAppDomainProxy.ExecuteCore(Stopwatch watch, ProfilerExecutionReport report, Object instance, Object executionParameter)
at PluginProfiler.Library.AppDomainProxy.Execute(ProfilerExecutionConfiguration configuration, ProfilerExecutionReport report)</a:value>
</KeyValuePairOfstringanyType>
</ErrorDetails>
<Message>Type 'SharepointUpload.SpoAuthUtility+ContextInfo' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.</Message>
<Timestamp>2017-12-12T09:38:40.2094703Z</Timestamp>
<ExceptionSource i:nil="true" />
<InnerFault i:nil="true" />
<OriginalException i:nil="true" />
<TraceText i:nil="true" />
</OrganizationServiceFault>

*This post is locked for comments

I have the same question (0)
  • Pradeep47 Profile Picture
    50 on at

    Guys any suggestions ?

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Pradeep,

    How have you implemented this code? Is it custom workflow? did you change any of the code after downloading it?

    I have used the same code and is working fine for me.

  • Pradeep47 Profile Picture
    50 on at

    No, I haven't changed any code.  I am using it in the plugin , I am using only two helper classes "HTTPHelper.cs" and "SPOAuthUtility.cs". not using any Sharepoint DLL.

    Code is breaking at this line :

    DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(ContextInfo));
    ContextInfo ci = (ContextInfo)json.ReadObject(stream);   // here code is breaking

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    I would suggest to try this on a console application and then once it is working move to plugin/ custom workflow.

  • Pradeep47 Profile Picture
    50 on at

    Tried in Console same error :(

    31741.Untitled.png

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

    I just tried the console app and it worked as expected. Here are the steps I followed-

    1. Downloaded the solution.

    2. Created new console application (I used VS 2015).

    3. Add below existing files from the downloaded solution-

         HTTPHelper.cs

         ISharePointService.cs

         SPOAuthUtility.cs

         SPService.cs

    4. Added reference to System.Runtime.Serialization

    5. Added below code in the main method under program.cs

    ================

    static void Main(string[] args)

           {

               SPService spService = new SPService("sharepoint_username", "sharepoint_password");

               spService.CreateFolder("SharePoint_URL", "Account/ABC Account");

           }

    ================

    Hope this helps.

  • Verified answer
    Iswarya Profile Picture
    1,347 on at

    hi,

    are u sure, have u add runtime serialization reference

    hjhjhjhjhj.png

  • Pradeep47 Profile Picture
    50 on at

    i was missing that .. I included that and json error gone .. orginal error still remains same

  • Pradeep47 Profile Picture
    50 on at

    Thanks alot Ravi,.. that really helped, I am able to create the folder using console app. hope this works in plugin as well. I need to upload the file in the the folder that's the main requirement.  can you please suggest me

    I need to get the folder name from the document location for policy record, if the folder is already available i need to upload the file into it, otherwise i need to create the folder and upload

    and thanks again for the help.

    I would like to try something like this :

    byte[] content = fileContent;

               string digest = _spo.GetRequestDigest();

               Uri url = new Uri(String.Format("{0}/_api/web/GetFolderByServerRelativeUrl('/{1}')/Files/Add(url='{2}', overwrite=true)", _spo.SiteUrl, propertyLibraryName + "/" + destLocation, _fileName));

               // Set X-RequestDigest

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

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

               webRequest.Headers.Add("Accept", "application/json;odata=verbose");

               webRequest.ContentLength = fileContent.Length;

               // 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

                 content,

                 webRequest,

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

              );

    T

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

    I would suggest before trying the upload file code, try to upload this create folder code as plugin and see if it works.

    Basically, get ready with your base framework ready i.e. connecting to SharePoint online from crm online using plugin. Rest all with be SharePoint REST API methods.

    For uploading file, I used the below blog but had to change some code line (don't remember which one's :( )

    www.inogic.com/.../programmatically-create-folders-and-upload-files-in-sharepoint-server-2013-through-plug-inworkflow

    Let me know if this doesn't work

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