Wednesday 9 January 2013

MS CRM 2011 integration with SharePoint 2010 !!!


MS CRM 2011 provides OOB support for SharePoint integration.This is good post about OOB SharePoint integration.
http://blogs.msdn.com/b/crm/archive/2010/10/08/crm-with-sharepoint-integration-introduction.aspx

We got a new requirement from one of our client where client want to create new documents folders in different SharePoint libraries.

I found good post about SharePoint Client Object Model. Following topics are covered on this post
  1. How to connect with SharePoint 2010 using SharePoint Client Object Model.
  2. How to retrieve SharePoint 2010 documents libraries.
  3. How to create folder in a document library


Download SharePoint Client Object Model and add the reference of following API in your project

Microsoft.SharePoint.Client
Microsoft.SharePoint.Client.Runtime

Here is the sample code.

//create client context
ClientContext _clientContext = new ClientContext(“http://server/SharePointDev”);
_clientContext.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
//create web object
Web web = _clientContext.Web;
_clientContext.Load(web);
_clientContext.ExecuteQuery();

 //read entire document libraries from SharePoint site
var DocumentLibraries = _clientContext.LoadQuery(web.Lists.Where(p => p.BaseType == BaseType.DocumentLibrary && p.Hidden == false));
_clientContext.ExecuteQuery();

If you want to read specific libraries then code will be like
var DocumentLibraries = _clientContext.LoadQuery(web.Lists.Where(p => p.BaseType == BaseType.DocumentLibrary && p.Hidden == false && (p.Title == " library1 " || p.Title == " library2")));
_clientContext.ExecuteQuery();

// check if document libraries are available
if (DocumentLibraries.Count() > 0)
{
foreach (var DocumentLibrary in DocumentLibraries)
{
//Inside each library, create a folder
CreateFolder(_clientContext, web, DocumentLibrary.Title, _FolderName);

//Get Library URL
string url = DocumentLibrary.DefaultViewUrl;
int index = url.LastIndexOf("/Forms/");
url = url.Substring(0, index);
int index1 = url.LastIndexOf("/");
string libURLName = url.Substring(index1 + 1);

//Create SharePoint Document Location Record in CRM.
//string _CreatedFolderPath=_siteUrl+"/"+DocumentLibrary.Title+"/"+_FolderName;
string _CreatedFolderPath = _siteUrl + "/" + libURLName + "/" + _FolderName;
CreateDocumentLocationRecord(CrmService, DocumentLibrary.Title, _CreatedFolderPath, entity.Id);
}
}

private List GetDocumentLibrary(ClientContext _clientContext, Web web, string libraryName)
{
try
{
if (web != null)
{
var query = _clientContext.LoadQuery(
web.Lists.Where(p => p.Title == libraryName));
_clientContext.ExecuteQuery();
return query.FirstOrDefault();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return null;
}

private void CreateFolder(ClientContext _clientContext, Web web, string libraryName, string folder)
{
try
{
var list = GetDocumentLibrary(_clientContext, web, libraryName);
if (list != null)
{
var folders = list.RootFolder.Folders;
_clientContext.Load(folders);
_clientContext.ExecuteQuery();
var newFolder = folders.Add(folder);
_clientContext.ExecuteQuery();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}

private void CreateDocumentLocationRecord(IOrganizationService service,string _locationName,string _locationURL,Guid _regarding)
{
try
{
// Instantiate an account object.
Entity DocumentLocation = new Entity("sharepointdocumentlocation");
//set the fields values
DocumentLocation["name"] = _locationName;
DocumentLocation["regardingobjectid"] = new EntityReference("account", _regarding);
DocumentLocation["absoluteurl"] = _locationURL;
//create the record
Guid DocumentLocationid = service.Create(DocumentLocation);

}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}






3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Please have a look into this : http://bingsoft.wordpress.com/2013/06/19/crm-online-to-sharepoint-online-integration-using-rest-and-adfs/

    ReplyDelete
  3. but files that go into folders created , how do you recommend add?

    ReplyDelete