Create Folder On Share Point plugin

 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 CreateSharepointFolder1 : IPlugin

{

static SharePointOnlineCredentials creds;

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

creds = 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 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, “”, creds);

//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 (creds == null)

{

SecureString securePassword = new SecureString();

for (int i = 0; i < Sharepointpassword.Length; i++)

securePassword.AppendChar(Sharepointpassword[i]);


creds = new SharePointOnlineCredentials(SharepointUsername, securePassword);

}

return creds;

}

public void CreateFolder(string siteUrl, string listName, string folderlogicalName, string newfolderName, string relativePath, SharePointOnlineCredentials cred)

{

using (ClientContext clientContext = new ClientContext(siteUrl))

{

clientContext.Credentials = creds;

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();

}

}

}

Comments

Popular posts from this blog

Power Automate - Update Child record when parent record is updated by using power automate in MS Dynamics CRM

Create Folder On Share Point by using Power Automate In Dynamics CRM

Duplicate Detection Plugin code for Microsoft dynamics CRM