Integrate Sharepoint document link in C# webservice in CRM 2011 Online


Since the Microsoft has introduced sharepoint integration with CRM 2011, the customer have started asking n number of different possibilities on the sharepoint integration.

So I am sharing one of the unique requirement with you all related to retrieving the sharepoint documents related to the entity in CRM using C# web service.

Now the requirement was to develop a web service which will retrieve the data from multiple entities by passing some parameters and webservice should retrieve sharepoint documents inside the sub folder under the entity record in CRM.

This requirement was quite simple and straight when we see the data to be retrieved from CRM entities in c# web service but the challenge was to retrieve the associated sharepoint documents with the retrieved entity record because in CRM SDK there is no reference available for retrieving the sharepoint documents.

So the approach was to connect to the sharepoint site and get the documents by checking the record name under the entity folder in sharepoint. Like Folder Accountà then Folder Record Nameà then Image or document


Before copy paste the code please follow the perquisites


Add Microsoft.Sharepoint.Client.dll and Microsoft.Sharepoint.Client.Runtime.dll as a reference in VS project.

Add the below namespace in the code

using Microsoft.SharePoint.Client;

Configuration setting for connection:

static string server = ConfigurationManager.AppSettings[“CRMserver”];
static string siteURL = ConfigurationManager.AppSettings[“sharepointsiteURL”];
static string password = ConfigurationManager.AppSettings[“password”];
static string username = ConfigurationManager.AppSettings[“username”];

Call this method inside the webservice method

List<SharepointList> sharepointList = GetArray();

For Example to get the Image directory in the form of List to display in webservice


public List<PublicClass> GetWebservice()


List< PublicClass > pList = new List< PublicClass >();



List<SharepointList> sharepointList = GetArray();

using (serviceProxy = new OrganizationServiceProxy(config.OrganizationUri, config.HomeRealmUri, config.Credentials,                                 config.DeviceCredentials))


serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

IOrganizationService service = (IOrganizationService)serviceProxy;

string fetchXml = “<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’false’>”

+ “<entity name=’new_customentity’>”

+ “<attribute name=’new_customentityid’ />”

+ “<attribute name=’new_name’ />”

+ ” </entity>”

+ “</fetch>”;

EntityCollection collection = service.RetrieveMultiple(new FetchExpression(fetchXml));

foreach (Entity retrieve in collection.Entities)


PublicClass emp = new PublicClass ();

emp.new_name = retrieve.Attributes[“new_name”].ToString();

emp.Images = sharepointList.FindAll(x => x.RecordName == emp.pav_name && x.FolderName.StartsWith(“Image”));





catch (Exception ex)



return pList;


public class PublicClass


public Guid pav_homenewid { get; set; }

public string pav_name { get; set; }

public List<SharepointList> Images { get; set; }


Copy the below method in your code to get sharepoint document in the form of List<T>

private List<SharepointList> GetArray()
SecureString securePassword = new SecureString();
for (int i = 0; i < password.Length; i++)
SharePointOnlineCredentials creds = new SharePointOnlineCredentials(username, securePassword);

ClientContext ctx = new ClientContext(siteURL);
ctx.Credentials = creds;
Web webSite = ctx.Web;

SP.List targetList = webSite.Lists.GetByTitle(<Entity Display Name in sharepoint>);

CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
ListItemCollection collListItem = targetList.GetItems(query);


List<SharepointList> spList = new List<SharepointList>();
foreach (ListItem oListItem in collListItem)
string[] splitt = oListItem[“FileRef”].ToString().Split(new char[] { ‘/’ });
string[] splittImage = splitt[splitt.Length – 1].ToString().Split(new char[] { ‘.’ });
if (splittImage.Length > 1)

int lnth = splitt.Length;

if (lnth == 5)

string EntityName = splitt[lnth – (lnth – 1)].ToString();
string RecordName = splitt[lnth – (lnth – 2)].ToString();
string FolderName = splitt[lnth – (lnth – 3)].ToString();
string ImageUrl = siteURL + oListItem[“FileRef”].ToString();

SharepointList shp = new SharepointList();
shp.EntityName = EntityName;
shp.RecordName = RecordName;
shp.FolderName = FolderName;
shp.ImageUrl = ImageUrl;

return spList;

public class SharepointList
public string EntityName { get; set; }
public string RecordName { get; set; }
public string FolderName { get; set; }
public string ImageUrl { get; set; }


I hope this will help somebody.

Namaste 🙂



6 thoughts on “Integrate Sharepoint document link in C# webservice in CRM 2011 Online

  1. Hi,

    Thanks for your article
    I add Microsoft.Sharepoint.Client.dll and Microsoft.Sharepoint.Client.Runtime.dll with SharePoint Server 2013 Client Components SDK.
    The dll are well referenced in the GAC.

    but when I trigger the workflow, I have this Exception :

    Could not load file or assembly ‘Microsoft.SharePoint.Client.Runtime, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ or one of its dependencies. Le fichier spécifié est introuvable.

    CPageCRM.Workflow: CPageCRM.Workflow.QuoteSendMailNotificationRIP]
    [Quote: QuoteSendMailNotificationRIP]

    Error Message:

    Unhandled Exception: System.IO.FileNotFoundException: Impossible de charger le fichier ou l’assembly ‘Microsoft.SharePoint.Client.Runtime, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c’ ou une de ses dépendances. Le fichier spécifié est introuvable.
    à CPageCRM.Workflow.QuoteSendMailNotificationRIP.Execute(CodeActivityContext executionContext)
    à System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
    à System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

    Could you help me please?

    • Hi Michael,

      Try to copy and paste both dll’s in bin/debugg folder and then build the project. And also change your project .net version to .net 4.0 instead of 3.5 and then check if it works for you.


  2. How can we integrate an entity with multi share point online document library.
    We are facing throttling issue. so now we have to create another document library to store the files.

  3. Hi , Can i get the full project to understand this and implement according to my requirement as i am new to this.

    • Drop me your email id and i will send the visual studio solution. I don’t have actual solution but recently i moved notes attachment to Sharepoint site in c# code using
      Microsoft.Sharepoint.Client.FileCreationInformation classes.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s