public virtual EntityPart CreateEntityPart(string containerTitle, string path, Guid entityId, string partName, string category, string data) { CheckIfFileNameIsReserved(partName + Constants.DocumentSetEntityPartExtension); SPSite site; var web = GetDocumentStoreWeb(out site); SPList list; SPFolder folder; if (SPDocumentStoreHelper.TryGetFolderFromPath(web, containerTitle, out list, out folder, path) == false) { return(null); } DocumentSet documentSet; if (SPDocumentStoreHelper.TryGetDocumentStoreEntityDocumentSet(list, folder, entityId, out documentSet) == false) { return(null); } var entityPartContentTypeId = new SPContentTypeId(Constants.DocumentStoreEntityPartContentTypeId); var listEntityPartContentTypeId = list.ContentTypes.BestMatch(entityPartContentTypeId); var entityPartContentType = list.ContentTypes[listEntityPartContentTypeId]; if (entityPartContentType == null) { throw new InvalidOperationException("Unable to locate the entity part content type"); } var properties = new Hashtable { { "ContentTypeId", entityPartContentType.Id.ToString() }, { "Content Type", entityPartContentType.Name } }; if (String.IsNullOrEmpty(category) == false) { properties.Add("DocumentEntityCategory", category); } web.AllowUnsafeUpdates = true; try { if (SPBaristaContext.Current.Web.CurrentUser == null) { throw new InvalidOperationException("User is not authenticated."); } if (documentSet.Item.DoesUserHavePermissions(SPBaristaContext.Current.Web.CurrentUser, SPBasePermissions.AddListItems) == false) { throw new InvalidOperationException("Insufficent Permissions."); } var partFileName = partName + Constants.DocumentSetEntityPartExtension; var existingEntityPart = list.ParentWeb.GetFile(SPUtility.ConcatUrls(documentSet.Folder.Url, partFileName)); //double-check lock pattern to prevent race-condition when creating an entity part. if (existingEntityPart.Exists == false) { var mutex = SPEntityMutexManager.GrabMutex(this.DocumentStoreUrl, entityId); mutex.WaitOne(); try { existingEntityPart = list.ParentWeb.GetFile(SPUtility.ConcatUrls(documentSet.Folder.Url, partFileName)); if (existingEntityPart.Exists == false) { var partFile = documentSet.Folder.Files.Add(partFileName, System.Text.Encoding.Default.GetBytes(data), properties, true); var entityPart = SPDocumentStoreHelper.MapEntityPartFromSPFile(partFile, data); //Update the content Entity Part string contentHash; DateTime contentModified; SPDocumentStoreHelper.CreateOrUpdateContentEntityPart(web, list, partFile.ParentFolder, null, entityPart, out contentHash, out contentModified); documentSet.Folder.Item["DocumentEntityContentsHash"] = contentHash; documentSet.Folder.Item["DocumentEntityContentsLastModified"] = contentModified; documentSet.Folder.Item.UpdateOverwriteVersion(); return(entityPart); } } finally { mutex.ReleaseMutex(); } } } finally { web.AllowUnsafeUpdates = false; } //An entity part already exists. throw new EntityPartExistsException("An entity part with the specified name already exists."); }
public virtual EntityPart UpdateEntityPart(string containerTitle, string path, Guid entityId, string partName, string category) { var mutex = SPEntityMutexManager.GrabMutex(this.DocumentStoreUrl, entityId); mutex.WaitOne(); try { SPSite site; var web = GetDocumentStoreWeb(out site); SPList list; SPFolder folder; if (SPDocumentStoreHelper.TryGetFolderFromPath(web, containerTitle, out list, out folder, path) == false) { return(null); } SPFile entityPartFile; if ( SPDocumentStoreHelper.TryGetDocumentStoreEntityPart(list, folder, entityId, partName, out entityPartFile) == false) { return(null); } web.AllowUnsafeUpdates = true; try { if (SPBaristaContext.Current.Web.CurrentUser == null) { throw new InvalidOperationException("User is not authenticated."); } if (entityPartFile.ParentFolder.Item.DoesUserHavePermissions(SPBaristaContext.Current.Web.CurrentUser, SPBasePermissions.EditListItems) == false) { throw new InvalidOperationException("Insufficent Permissions."); } entityPartFile.Item["DocumentEntityCategory"] = category; entityPartFile.Item.SystemUpdate(true); var entityPart = SPDocumentStoreHelper.MapEntityPartFromSPFile(entityPartFile, null); //Update the content entity part string contentHash; DateTime contentModified; SPDocumentStoreHelper.CreateOrUpdateContentEntityPart(web, list, entityPartFile.ParentFolder, null, entityPart, out contentHash, out contentModified); var documentSetFolder = web.GetFolder(entityPartFile.ParentFolder.UniqueId); documentSetFolder.Item["DocumentEntityContentsHash"] = contentHash; documentSetFolder.Item["DocumentEntityContentsLastModified"] = contentModified; documentSetFolder.Item.UpdateOverwriteVersion(); return(entityPart); } finally { web.AllowUnsafeUpdates = false; } } finally { mutex.ReleaseMutex(); } }
public virtual EntityPart UpdateEntityPartData(string containerTitle, string path, Guid entityId, string partName, string eTag, string data) { var mutex = SPEntityMutexManager.GrabMutex(this.DocumentStoreUrl, entityId); mutex.WaitOne(); try { SPSite site; var web = GetDocumentStoreWeb(out site); SPList list; SPFolder folder; if (SPDocumentStoreHelper.TryGetFolderFromPath(web, containerTitle, out list, out folder, path) == false) { return(null); } SPFile entityPartFile; if ( SPDocumentStoreHelper.TryGetDocumentStoreEntityPart(list, folder, entityId, partName, out entityPartFile) == false) { return(null); } if (String.IsNullOrEmpty(eTag) == false && entityPartFile.ETag != eTag) { throw new InvalidOperationException( string.Format( "Could not update the entity part, the entity part has been updated by another user. New:{0} Existing{1}", eTag, entityPartFile.ETag)); } web.AllowUnsafeUpdates = true; try { var currentData = entityPartFile.Web.GetFileAsString(entityPartFile.Url); var entityPart = SPDocumentStoreHelper.MapEntityPartFromSPFile(entityPartFile, data); if (data != currentData) { entityPartFile.SaveBinary(String.IsNullOrEmpty(data) == false ? System.Text.Encoding.Default.GetBytes(data) : System.Text.Encoding.Default.GetBytes(String.Empty)); //Update the content entity part string contentHash; DateTime contentModified; SPDocumentStoreHelper.CreateOrUpdateContentEntityPart(web, list, entityPartFile.ParentFolder, null, entityPart, out contentHash, out contentModified); var documentSetFolder = web.GetFolder(entityPartFile.ParentFolder.UniqueId); documentSetFolder.Item["DocumentEntityContentsHash"] = contentHash; documentSetFolder.Item["DocumentEntityContentsLastModified"] = contentModified; documentSetFolder.Item.UpdateOverwriteVersion(); } return(entityPart); } finally { web.AllowUnsafeUpdates = false; } } finally { mutex.ReleaseMutex(); } }
/// <summary> /// Creates a new entity in the document store, contained in the specified container in the specified folder and namespace. /// </summary> /// <param name="containerTitle">The container title. Required.</param> /// <param name="path">The path. Optional.</param> /// <param name="title">The title of the entity. Optional.</param> /// <param name="namespace">The namespace of the entity. Optional.</param> /// <param name="data">The data to store with the entity. Optional.</param> /// <returns></returns> public virtual Entity CreateEntity(string containerTitle, string path, string title, string @namespace, string data) { if (data == null) { data = String.Empty; } SPSite site; var web = GetDocumentStoreWeb(out site); SPList list; SPFolder folder; if (SPDocumentStoreHelper.TryGetFolderFromPath(web, containerTitle, out list, out folder, path) == false) { throw new InvalidOperationException("Unable to retrieve the specified folder -- Folder does not exist."); } var newGuid = Guid.NewGuid(); var entityTitle = String.IsNullOrEmpty(title) ? newGuid.ToString() : title; var docEntityContentTypeId = list.ContentTypes.BestMatch(new SPContentTypeId(Constants.DocumentStoreEntityContentTypeId)); var properties = new Hashtable { { "DocumentSetDescription", "Document Store Entity" }, { "DocumentEntityGuid", newGuid.ToString() }, { "Namespace", @namespace } }; web.AllowUnsafeUpdates = true; try { if (SPBaristaContext.Current.Web.CurrentUser == null) { throw new InvalidOperationException("User is not authenticated."); } if ((folder.Item == null && list.RootFolder == folder && list.DoesUserHavePermissions(SPBaristaContext.Current.Web.CurrentUser, SPBasePermissions.AddListItems) == false) || (folder.Item != null && (folder.Item.DoesUserHavePermissions(SPBaristaContext.Current.Web.CurrentUser, SPBasePermissions.AddListItems) == false))) { throw new InvalidOperationException("Insufficient Permissions."); } DocumentSet documentSet; if (PermissionsHelper.IsRunningUnderElevatedPrivledges(site.WebApplication.ApplicationPool)) { var existingEntity = list.ParentWeb.GetFile(SPUtility.ConcatUrls(folder.Url, entityTitle)); //Double check locking if (existingEntity.Exists == false) { var mutex = SPEntityMutexManager.GrabMutex(this.DocumentStoreUrl, newGuid); mutex.WaitOne(); try { existingEntity = list.ParentWeb.GetFile(SPUtility.ConcatUrls(folder.Url, entityTitle)); if (existingEntity.Exists == false) { var currentUser = web.AllUsers[CurrentUserLoginName]; documentSet = DocumentSet.Create(folder, entityTitle, docEntityContentTypeId, properties, false, currentUser); //Re-retrieve the document set folder, otherwise bad things happen. var documentSetFolder = web.GetFolder(documentSet.Folder.Url); documentSet = DocumentSet.GetDocumentSet(documentSetFolder); var entityPartContentTypeId = new SPContentTypeId(Constants.DocumentStoreEntityPartContentTypeId); var listEntityPartContentTypeId = list.ContentTypes.BestMatch(entityPartContentTypeId); var entityPartContentType = list.ContentTypes[listEntityPartContentTypeId]; var entityPartProperties = new Hashtable { { "ContentTypeId", entityPartContentType.Id.ToString() }, { "Content Type", entityPartContentType.Name } }; documentSet.Folder.Files.Add(Constants.DocumentStoreDefaultEntityPartFileName, Encoding.Default.GetBytes(data), entityPartProperties, currentUser, currentUser, DateTime.UtcNow, DateTime.UtcNow, true); //Update the contents entity part and the modified by user stamp. string contentHash; DateTime contentModified; SPDocumentStoreHelper.CreateOrUpdateContentEntityPart(web, list, documentSet.Folder, null, null, out contentHash, out contentModified); //Set the created/updated fields of the new document set to the current user. var userLogonName = currentUser.ID + ";#" + currentUser.Name; documentSet.Item[SPBuiltInFieldId.Editor] = userLogonName; documentSet.Item["DocumentEntityContentsHash"] = contentHash; documentSet.Item["DocumentEntityContentsLastModified"] = contentModified; documentSet.Item.UpdateOverwriteVersion(); return(SPDocumentStoreHelper.MapEntityFromDocumentSet(documentSet, data)); } } finally { mutex.ReleaseMutex(); } } } else { var existingEntity = list.ParentWeb.GetFile(SPUtility.ConcatUrls(folder.Url, entityTitle)); //Double check locking if (existingEntity.Exists == false) { var mutex = SPEntityMutexManager.GrabMutex(this.DocumentStoreUrl, newGuid); mutex.WaitOne(); try { existingEntity = list.ParentWeb.GetFile(SPUtility.ConcatUrls(folder.Url, entityTitle)); if (existingEntity.Exists == false) { documentSet = DocumentSet.Create(folder, entityTitle, docEntityContentTypeId, properties, false); //Re-retrieve the document set folder, otherwise bad things happen. var documentSetFolder = web.GetFolder(documentSet.Folder.Url); documentSet = DocumentSet.GetDocumentSet(documentSetFolder); var entityPartContentTypeId = new SPContentTypeId(Constants.DocumentStoreEntityPartContentTypeId); var listEntityPartContentTypeId = list.ContentTypes.BestMatch(entityPartContentTypeId); var entityPartContentType = list.ContentTypes[listEntityPartContentTypeId]; var entityPartProperties = new Hashtable { { "ContentTypeId", entityPartContentType.Id.ToString() }, { "Content Type", entityPartContentType.Name } }; documentSet.Folder.Files.Add(Constants.DocumentStoreDefaultEntityPartFileName, Encoding.Default.GetBytes(data), entityPartProperties, true); //Update the contents Entity Part. string contentHash; DateTime contentModified; SPDocumentStoreHelper.CreateOrUpdateContentEntityPart(web, list, documentSet.Folder, null, null, out contentHash, out contentModified); documentSet.Item["DocumentEntityContentsHash"] = contentHash; documentSet.Item["DocumentEntityContentsLastModified"] = contentModified; documentSet.Item.UpdateOverwriteVersion(); return(SPDocumentStoreHelper.MapEntityFromDocumentSet(documentSet, data)); } } finally { mutex.ReleaseMutex(); } } } } finally { web.AllowUnsafeUpdates = false; } throw new EntityExistsException("An entity with the specified title exists."); }