Close

BLOGS

Create folder in sharepoint site from CRM record via plugin

Add the below sharepoint dll’s as reference in the CRM plugin.

  1. Microsoft.SharePoint.Client.dll
  2. 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();
}
}
}

Print
Posted: Apr 16, 2018,
Comments: 0,
Author: Urish Arora
Rate this article:
5.0

Urish AroraUrish Arora

Other posts by Urish Arora

, Contact author

Please login or register to post comments.

Name:
Email:
Subject:
Message:
x

SEARCH

Categories

«January 2021»
MonTueWedThuFriSatSun
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

  • Featured Posts
  • Recent Posts
  • Recent Comments
Dynamics365Authority is a community platform for Professionals and Students to contribute or share their knowledge and skills on Microsoft Dynamics 365 Technologies. Our primary goal is to invite professionals on Dynamics 365 technology across the world to contribute & share their knowledge and skills through their blogs so that we can help other developers.
Stay updated with Dynamics 365 technology. Improve skills and knowledge from our blogs, articles and code snippets. Learn it. Follow professionals and learn from them.
Unified Service Desk Book(Online Edition) 

FOLLOW US

Stay connected with us on our social media channels for latest articles, blogs posts etc.  We will keep updating regularly on our social media platform and web platform.

FACEBOOK

Follow us on Facebook

 

TWITTER

View all our tweets

 

DYNAMICS365AUTH

Join us now

 

BLOGS

View our blogs

 

 

Latest Blog

Posted: Dec 24, 2020

Form Event Programming Using JavaScript in Dynamics 365 - An Overview

In this video you will see the details on 'Form Event Programming using JavaScript in Dynamics 365'. This will also explore the different areas where you can use JavaScript in Dynamics 365. 

Read more
Posted: Dec 24, 2020

Getting Started with PowerApps

This video is a complete package of about the Power Apps. This video starts with the discussion about What is Power Apps and key reasons to use them. The difference between Canvas Apps and Model Driven Apps. In third part you will see a demo on how to create a Canvas App. Similarly in the forth part you will see the demo on how to create a Model Driven App. There is also a topic What is a Common Data Service (CDS) which will be covered in the fifth part and in the last part you will have some useful information about the PowerApps Portal (aka CDS Starter Portal).

Read more
Posted: Dec 23, 2020

Business Rules vs JavaScript Client API Dynamics 365

This video is about the difference between the Business Rules and JavaScript Client API in Dynamics 365 and some limitation of Business Rules over JavaScript Client API.

Read more
RSS

ADDITIONAL RESOURCES

Dynamics 365 Authority.com - Search the library for "Dynamics 365"

  What's New in Dynamics 365

  Dynamics 365 Documentation

  

  Sign-up for 30 days trial

 

 

Recent comments

Ashish:

Nice blog. Very helpful to get all references to white papers at one place.

Previous Next

About Us

Here comes the Dynamics 365 Authority - Great technical blog posts are hidden gems. They are hard to find simply because not enough of us write them in the first place. Yet technical blogging is one of the best things we can do not only for ourselves, but also as members of the wider community of developers.

Dynamics 365 Authority leverages this platform to help developers and users to move from traditional paper-based process to modern digital business process. Dynamics365Authority.com helps your business grow, evolve and transform.

Dynamics365Authority.com is your Digital Transformation Partner

Recent Posts

Form Event Programming Using JavaScript in Dynamics 365 - An Overview

In this video you will see the details on 'Form Event Programming using JavaScript in Dynamics 365'. This will also explore the different areas where you can use JavaScript in Dynamics...
  • 242
  • Article rating: 5.0

Getting Started with PowerApps

This video is a complete package of about the Power Apps. This video starts with the discussion about What is Power Apps and key reasons to use them. The difference between Canvas Apps and Model...
  • 208
  • Article rating: 5.0
RSS

Follow Us

 

Tag cloud: Dynamics365Authority.com; Dynamics365Authority.com; Sales; Leading community site on MS Dynamics 365; Dynamics 365 Authority; Service;Top CRM Blogs;  Digital Transformation; Field Service;Top Articles on MS Dynamics CRM Relationship Insights; Office 365 Security Compliance; Project Service; Microsoft Dynamics 365 for Sales; Relevance Search; Goal Metrics; Activities and Case; Sales Literature;Knowledge Management;

Terms Of UsePrivacy StatementCopyright 2021 by Dynamics365Authority
Back To Top