Hi, today I will talk about Connect CRM to SharePoint via SDK. And then write the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
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 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(); } } } |
SharePoint 2013 Hosting Recommendation
HostForLIFE.eu’s SharePoint 2013 Hosting solution offers a comprehensive feature set that is easy-to-use for new users, yet powerful enough for the most demanding web developer expert. Hosted SharePoint Foundation 2013 is the premiere web-based collaboration and productivity enhancement tool on the market today. With SharePoint 2013 Foundation, you can quickly access and manage documents and information anytime, anywhere though a Web browser in a secure and user friendly way. SharePoint hosting services start at only at €9.99/mo, allowing you to take advantage of the robust feature set for a small business price. HostForLIFE.eu offers a variety of hosted SharePoint Foundation 2013 plans as well as dedicated SharePoint 2013 Foundation options.