public SharePointDocumentLocation GetOrCreateDocumentLibraryLocation(SharePointSite site, string documentLibraryName) { using (var context = new OrganizationServiceContext(_crmService)) { // Get location SharePointDocumentLocation docLocation = (from d in context.CreateQuery <SharePointDocumentLocation>() where d.ParentSiteOrLocation.Id == site.Id && d.Name == documentLibraryName select new SharePointDocumentLocation { Id = d.Id, Name = d.Name, ParentSiteOrLocation = d.ParentSiteOrLocation, RegardingObjectId = d.RegardingObjectId, RelativeUrl = d.RelativeUrl }).FirstOrDefault(); if (docLocation == null) { // Create if not already docLocation = new SharePointDocumentLocation { Name = documentLibraryName, ParentSiteOrLocation = site.ToEntityReference(), RelativeUrl = documentLibraryName }; docLocation.Id = _crmService.Create(docLocation); } return(docLocation); } }
/// <summary> /// Creates any entity records that this sample requires. /// </summary> public void CreateRequiredRecords() { // Instantiate a SharePoint site object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointSite spSite = new SharePointSite { Name = "Sample SharePoint Site", Description = "Sample SharePoint Site Location record", AbsoluteURL = _siteAbsoluteURL, IsGridPresent = true }; // Create a SharePoint site record named Sample SharePoint Site. _spSiteId = _serviceProxy.Create(spSite); Console.WriteLine("{0} created.", spSite.Name); // Instantiate a SharePoint document location object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointDocumentLocation spDocLoc = new SharePointDocumentLocation { Name = "Sample SharePoint Document Location", Description = "Sample SharePoint Document Location record", // Set the Sample SharePoint Site created earlier as the parent site. ParentSiteOrLocation = new EntityReference(SharePointSite.EntityLogicalName, _spSiteId), RelativeUrl = "spdocloc" }; // Create a SharePoint document location record named Sample SharePoint Document Location. _spDocLocId = _serviceProxy.Create(spDocLoc); Console.WriteLine("{0} created.", spDocLoc.Name); }
private List <SharePointDocumentLocation> GetDocumentLocationPath(SharePointDocumentLocation parentLocation) { var locations = new List <SharePointDocumentLocation>(); using (var context = new OrganizationServiceContext(_crmService)) { context.MergeOption = MergeOption.NoTracking; SharePointDocumentLocation location = parentLocation; EntityReference parent = null; do { // Recursively get the sharepoint document locations using parent location/site location = (from l in context.CreateQuery <SharePointDocumentLocation>() where l.Id == location.ParentSiteOrLocation.Id select new SharePointDocumentLocation { Id = l.Id, Name = l.Name, RelativeUrl = l.RelativeUrl, ParentSiteOrLocation = l.ParentSiteOrLocation }).FirstOrDefault(); if (location != null) { locations.Add(location); } parent = (location != null) ? location.ParentSiteOrLocation : null; } while (parent != null); } return(locations); }
public SharePointDocumentLocation GetOrCreateDocumentLibraryLocationByParentId(Guid parentObjectId, string RelativeUrl) { using (var context = new OrganizationServiceContext(_crmService)) { // Get location SharePointDocumentLocation docLocation = (from d in context.CreateQuery <SharePointDocumentLocation>() where d.ParentSiteOrLocation.Id == parentObjectId && d.RelativeUrl == RelativeUrl select new SharePointDocumentLocation { Id = d.Id, Name = d.Name, ParentSiteOrLocation = d.ParentSiteOrLocation, RegardingObjectId = d.RegardingObjectId, RelativeUrl = d.RelativeUrl }).FirstOrDefault(); if (docLocation == null) { // Create if not already docLocation = new SharePointDocumentLocation { Name = "Cedant Programs", ParentSiteOrLocation = new EntityReference("sharepointdocumentlocation", parentObjectId), RelativeUrl = RelativeUrl }; docLocation.Id = _crmService.Create(docLocation); } return(docLocation); } }
private void CreateSharePointDocumentLibrary(SharePointSite site, SharePointDocumentLocation spDocumentLocation) { var folderPath = GetDocumentLocationPath(spDocumentLocation); // Create sharepoint folder var folderPathString = String.Join("/", folderPath.ToArray().Reverse().Select(a => a.RelativeUrl)) + "/" + spDocumentLocation.RelativeUrl; _spService.CreateFolder(site.AbsoluteURL, folderPathString); spDocumentLocation.AbsoluteURL = folderPathString; // Set temporaryily in memory to allow the caller to get the full path }
public SharePointDocumentLocation CreateDocumentLocation(SharePointSite site, string documentLibraryName, EntityReference regardingRecord) { if (regardingRecord != null && string.IsNullOrEmpty(regardingRecord.Name)) { throw new InvalidPluginExecutionException("Document location of regarding record must have a name"); } // Does the document location already exist for this record and site? var existingDocumentLocation = this.GetDocumentLocationForRecordBySite(site, regardingRecord); if (existingDocumentLocation != null) { return(existingDocumentLocation); } var parentDocumentLocation = GetOrCreateDocumentLibraryLocation(site, documentLibraryName).ToEntityReference(); // Create document location for folder SharePointDocumentLocation docLoc = new SharePointDocumentLocation(); docLoc.Name = regardingRecord.Name; docLoc.RelativeUrl = GetFolderName(regardingRecord); docLoc.RegardingObjectId = regardingRecord; docLoc.ParentSiteOrLocation = parentDocumentLocation; docLoc.Id = _crmService.Create(docLoc); var folderPath = GetDocumentLocationPath(docLoc); // Create sharepoint folder var folderPathString = String.Join("/", folderPath.ToArray().Reverse().Select(a => a.RelativeUrl)) + "/" + docLoc.RelativeUrl; _spService.CreateFolder(site.AbsoluteURL, folderPathString); docLoc.AbsoluteURL = folderPathString; // Set temporaryily in memory to allow the caller to get the full path return(docLoc); }
/// <summary> /// This method first connects to the Organization service. Afterwards, /// create, retrieve, update, and delete operations are performed on the /// SharePoint location records. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); CreateRequiredRecords(); // Instantiate a SharePoint site object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointSite spSite = new SharePointSite { Name = "Sample SharePoint Site", Description = "Sample SharePoint Site Location record", // TODO: Change this URL to a valid SharePoint URL. AbsoluteURL = "https://www.example.com", }; // Create a SharePoint site record named Sample SharePoint Site. _spSiteId = _serviceProxy.Create(spSite); Console.WriteLine("{0} created.", spSite.Name); // Instantiate a SharePoint document location object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointDocumentLocation spDocLoc = new SharePointDocumentLocation { Name = "Sample SharePoint Document Location", Description = "Sample SharePoint Document Location record", // Set the Sample SharePoint Site created earlier as the parent site. ParentSiteOrLocation = new EntityReference(SharePointSite.EntityLogicalName, _spSiteId), RelativeUrl = "spdocloc", // Associate this document location instance with the Fourth Coffee // sample account record. RegardingObjectId = new EntityReference(Account.EntityLogicalName, _account1Id) }; // Create a SharePoint document location record named Sample SharePoint Document Location. _spDocLocId = _serviceProxy.Create(spDocLoc); Console.WriteLine("{0} created.", spDocLoc.Name); // Retrieve the SharePoint site and SharePoint document location containing several of its attributes. ColumnSet colsSpSite = new ColumnSet("name", "absoluteurl"); SharePointSite retrievedSpSite = (SharePointSite)_serviceProxy.Retrieve(SharePointSite.EntityLogicalName, _spSiteId, colsSpSite); ColumnSet colsSpDocLoc = new ColumnSet("name", "regardingobjectid"); SharePointDocumentLocation retrievedSpDocLoc = (SharePointDocumentLocation)_serviceProxy.Retrieve(SharePointDocumentLocation.EntityLogicalName, _spDocLocId, colsSpDocLoc); Console.Write("Retrieved,"); // Update the URL of the SharePoint site. // TODO: Change this URL to a valid SharePoint URL. retrievedSpSite.AbsoluteURL = "https://www.example.net"; _serviceProxy.Update(retrievedSpSite); // Update the SharePoint document location to associate it with the // Northwind Traders sample account. retrievedSpDocLoc.RegardingObjectId = new EntityReference(Account.EntityLogicalName, _account2Id); _serviceProxy.Update(retrievedSpDocLoc); Console.WriteLine(" and updated the records."); DeleteRequiredRecords(promptforDelete); } } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ) { // You can handle an exception here or pass it back to the calling method. throw; } }
/// <summary> /// This method first connects to the Organization service. Afterwards, /// create, retrieve, update, and delete operations are performed on the /// SharePoint location records. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptforDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete) { try { //<snippetCRUDSharePointLocationRecords1> // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); CreateRequiredRecords(); //<snippetCRUDSharePointLocationRecords2> // Instantiate a SharePoint site object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointSite spSite = new SharePointSite { Name = "Sample SharePoint Site", Description = "Sample SharePoint Site Location record", // TODO: Change this URL to a valid SharePoint URL. AbsoluteURL = "http://www.example.com", }; // Create a SharePoint site record named Sample SharePoint Site. _spSiteId = _serviceProxy.Create(spSite); //</snippetCRUDSharePointLocationRecords2> Console.WriteLine("{0} created.", spSite.Name); //<snippetCRUDSharePointLocationRecords3> // Instantiate a SharePoint document location object. // See the Entity Metadata topic in the SDK documentation to determine // which attributes must be set for each entity. SharePointDocumentLocation spDocLoc = new SharePointDocumentLocation { Name = "Sample SharePoint Document Location", Description = "Sample SharePoint Document Location record", // Set the Sample SharePoint Site created earlier as the parent site. ParentSiteOrLocation = new EntityReference(SharePointSite.EntityLogicalName, _spSiteId), RelativeUrl = "spdocloc", // Associate this document location instance with the Fourth Coffee // sample account record. RegardingObjectId = new EntityReference(Account.EntityLogicalName, _account1Id) }; // Create a SharePoint document location record named Sample SharePoint Document Location. _spDocLocId = _serviceProxy.Create(spDocLoc); //</snippetCRUDSharePointLocationRecords3> Console.WriteLine("{0} created.", spDocLoc.Name); // Retrieve the SharePoint site and SharePoint document location containing several of its attributes. ColumnSet colsSpSite = new ColumnSet("name", "absoluteurl"); SharePointSite retrievedSpSite = (SharePointSite)_serviceProxy.Retrieve(SharePointSite.EntityLogicalName, _spSiteId, colsSpSite); ColumnSet colsSpDocLoc = new ColumnSet("name", "regardingobjectid"); SharePointDocumentLocation retrievedSpDocLoc = (SharePointDocumentLocation)_serviceProxy.Retrieve(SharePointDocumentLocation.EntityLogicalName, _spDocLocId, colsSpDocLoc); Console.Write("Retrieved,"); // Update the URL of the SharePoint site. // TODO: Change this URL to a valid SharePoint URL. retrievedSpSite.AbsoluteURL = "http://www.example.net"; _serviceProxy.Update(retrievedSpSite); // Update the SharePoint document location to associate it with the // Northwind Traders sample account. retrievedSpDocLoc.RegardingObjectId = new EntityReference(Account.EntityLogicalName,_account2Id); _serviceProxy.Update(retrievedSpDocLoc); Console.WriteLine(" and updated the records."); DeleteRequiredRecords(promptforDelete); } //</snippetCRUDSharePointLocationRecords1> } // Catch any service fault exceptions that Microsoft Dynamics CRM throws. catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>) { // You can handle an exception here or pass it back to the calling method. throw; } }