Add the below sharepoint dll’s as reference in the CRM plugin.
- Microsoft.SharePoint.Client.dll
- Microsoft.SharePoint.Client.Runtime.dll
Note: This will only work for On-premise CRM not for CRM online because we don’t have access to CRM server physical directory to copy the above dll’s.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
using SP = Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client;
using System.Security;
public class createSharepointFolder : IPlugin
{
static SharePointOnlineCredentials credentials;
static string SharepointsiteURL = “”;
static string SharepointUsername = “”;
static string Sharepointpassword = “”;
public CreateSharepointFolder(string unSecureConfig, string secureConfig)
{
if (string.IsNullOrEmpty(unSecureConfig))
throw new InvalidPluginExecutionException(“Unsecure configuration missing.”);
else
{
SharepointsiteURL = unSecureConfig;
string[] splitt = secureConfig.Split(new char[] { ‘;’ });
SharepointUsername = secureConfig[0].ToString();
Sharepointpassword = secureConfig[1].ToString();
}
}
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
//Create connection to sharepoint site
credentials = getSharepointCredentials();
try
{
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains(“Target”) && context.InputParameters[“Target”] is Entity)
{
Entity entity = (Entity)context.InputParameters[“Target”];
if (context.MessageName == “Create” && context.PrimaryEntityName == “new_entity”)
{
//Entity folder name in sharepoint site
string entityDisplayName = “Test New Entity”;
//Folder name for new record
string RecordFolderName= entity.Attributes[“new_name”].ToString() + “_” + entity.Id.ToString().Replace(“{“, “”).Replace(“}”, “”).Replace(“-“, “”).ToUpper();
//Create entity main folder
CreateFolder(SharepointsiteURL, entityDisplayName, context.PrimaryEntityName.ToString(), RecordFolderName, “”, credentials);
//Create child folder
CreateFolder(SharepointsiteURL, entityDisplayName, context.PrimaryEntityName.ToString(), RecordFolderName, “Images”, creds);
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message.ToString());
}
}
public SharePointOnlineCredentials getSharepointCredentials()
{
if (credentials == null)
{
SecureString securePassword = new SecureString();
for (int i = 0; i < Sharepointpassword.Length; i++)
securePassword.AppendChar(Sharepointpassword[i]);
creds = new SharePointOnlineCredentials(SharepointUsername, securePassword);
}
return credentials;
}
public void createFolder(string siteUrl, string listName, string folderlogicalName, string newfolderName, string relativePath, SharePointOnlineCredentials cred)
{
using (ClientContext clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = credentials;
Web web = clientContext.Web;
SP.List list = web.Lists.GetByTitle(listName);
ListItemCreationInformation newItem = new ListItemCreationInformation();
newItem.UnderlyingObjectType = FileSystemObjectType.Folder;
newItem.FolderUrl = siteUrl + folderlogicalName;
if (!relativePath.Equals(string.Empty))
newItem.FolderUrl += “/” + relativePath;
newItem.LeafName = newfolderName;
SP.ListItem item = list.AddItem(newItem);
item.Update();
clientContext.ExecuteQuery();
}
}
}