private static void CreateFiles(ICommandContext commandContext, DirectoryUploadInfo uploadInfo, IEnumerable <HttpPostedFile> files, EntityReference publishingState, out List <string> @select, out List <Tuple <string, string> > errors) { @select = new List <string>(); errors = new List <Tuple <string, string> >(); var dataAdapterDependencies = new PortalConfigurationDataAdapterDependencies(); var annotationDataAdapter = new AnnotationDataAdapter(dataAdapterDependencies); var website = HttpContext.Current.GetWebsite(); var location = website.Settings.Get <string>("WebFiles/StorageLocation"); StorageLocation storageLocation; if (!Enum.TryParse(location, true, out storageLocation)) { storageLocation = StorageLocation.CrmDocument; } var maxFileSizeErrorMessage = website.Settings.Get <string>("WebFiles/MaxFileSizeErrorMessage"); var annotationSettings = new AnnotationSettings(dataAdapterDependencies.GetServiceContext(), storageLocation: storageLocation, maxFileSizeErrorMessage: maxFileSizeErrorMessage); foreach (var file in files) { var serviceContext = commandContext.CreateServiceContext(); try { var webFile = new Entity("adx_webfile"); var fileName = Path.GetFileName(file.FileName); webFile.Attributes["adx_name"] = fileName; webFile.Attributes["adx_partialurl"] = GetPartialUrlFromFileName(fileName); webFile.Attributes["adx_websiteid"] = website.Entity.ToEntityReference(); webFile.Attributes["adx_publishingstateid"] = publishingState; webFile.Attributes["adx_hiddenfromsitemap"] = true; webFile.Attributes[uploadInfo.WebFileForeignKeyAttribute] = uploadInfo.EntityReference; serviceContext.AddObject(webFile); serviceContext.SaveChanges(); annotationDataAdapter.CreateAnnotation(new Annotation { Regarding = webFile.ToEntityReference(), FileAttachment = AnnotationDataAdapter.CreateFileAttachment(new HttpPostedFileWrapper(file), annotationSettings.StorageLocation) }, annotationSettings); @select.Add(new DirectoryContentHash(webFile.ToEntityReference()).ToString()); } catch (Exception e) { ADXTrace.Instance.TraceError(TraceCategory.Application, string.Format(@"Exception uploading file: {0}", e.ToString())); errors.Add(new Tuple <string, string>(file.FileName, e.Message)); } } }
private static List <Tuple <string, string> > RemoveFiles(ICommandContext commandContext) { var errors = new List <Tuple <string, string> >(); var targetHashes = (commandContext.Parameters["targets[]"] ?? string.Empty).Split(','); if (!targetHashes.Any()) { return(errors); } var portal = commandContext.CreatePortalContext(); var website = portal.Website.ToEntityReference(); var security = commandContext.CreateSecurityProvider(); var dataServiceProvider = commandContext.CreateDependencyProvider().GetDependency <ICmsDataServiceProvider>(); foreach (var targetHash in targetHashes) { var serviceContext = commandContext.CreateServiceContext(); Entity target; if (!TryGetTargetEntity(serviceContext, targetHash, website, out target)) { errors.Add(new Tuple <string, string>(targetHash, ResourceManager.GetString("Unable_To_Retrieve_Target_Entity_For_Given_Hash_Error"))); continue; } try { OrganizationServiceContextInfo serviceContextInfo; EntitySetInfo entitySetInfo; if (dataServiceProvider != null && OrganizationServiceContextInfo.TryGet(serviceContext.GetType(), out serviceContextInfo) && serviceContextInfo.EntitySetsByEntityLogicalName.TryGetValue(target.LogicalName, out entitySetInfo)) { dataServiceProvider.DeleteEntity(serviceContext, entitySetInfo.Property.Name, target.Id); } else { if (!security.TryAssert(serviceContext, target, CrmEntityRight.Change)) { errors.Add(new Tuple <string, string>(GetDisplayName(target), ResourceManager.GetString("Delete_Permission_Denied_For_Target_Entity_Error"))); continue; } CrmEntityInactiveInfo inactiveInfo; if (CrmEntityInactiveInfo.TryGetInfo(target.LogicalName, out inactiveInfo)) { serviceContext.SetState(inactiveInfo.InactiveState, inactiveInfo.InactiveStatus, target); } else { serviceContext.DeleteObject(target); serviceContext.SaveChanges(); } } } catch (Exception e) { ADXTrace.Instance.TraceError(TraceCategory.Application, string.Format("{0} {1}", ResourceManager.GetString("Deleting_File_Exception"), e.ToString())); errors.Add(new Tuple <string, string>(GetDisplayName(target), e.Message)); } } return(errors); }