//TODO: GetxxxByID是否是个通用的逻辑? public DCTFile DCMGetFileById(int fileId) { (fileId <= 0).TrueThrow <ArgumentException>("ID({0})无效,请传入大于0的值.", fileId); using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { string uri = LoadUri(new DCTFile() { ID = fileId }, context); File file = context.Web.GetFileByServerRelativeUrl(uri); context.Load(file); context.Load(file.Author); context.ExecuteQuery(); if (file.CheckOutType != CheckOutType.None) { context.Load(file.CheckedOutByUser); } context.Load(file.ListItemAllFields); context.Load(file.ModifiedBy); context.ExecuteQuery(); DCTFile dctFile = new DCTFile(); dctFile.AbsoluteUri = UriHelper.CombinePath(context.Url, uri); DCTConverterHelper.Convert(file, dctFile); return(dctFile); } }
public DCTFolder DCMGetFolderByUri(string uri) { using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { Folder folder = clientContext.Web.GetFolderByServerRelativeUrl(uri); clientContext.Load(folder); CamlQuery query = new CamlQuery(); query.ViewXml = Caml.SimpleView(Caml.Field("FileRef").Eq(Caml.Value("Text", uri)), CamlBuilder.ViewType.RecursiveAll).ToCamlString(); ListItemCollection items = clientContext.BaseList.GetItems(query); clientContext.Load(items); DCTFolder result; try { clientContext.ExecuteQuery(); if (string.Compare(folder.ServerRelativeUrl, this.DCMGetRootFolder().Uri, true) == 0) result = new DCTFolder() { ID = 0, IsRootFolder = true }; else result = new DCTFolder() { ID = items[0].Id, IsRootFolder = false }; DCTConverterHelper.Convert(folder, result); } catch { result = null; } return result; } }
private static BaseCollection <DCTFileField> BuildFileFields(DocLibContext context, IEnumerable <Field> spfields, File file) { BaseCollection <DCTFileField> results = new BaseCollection <DCTFileField>(); context.Load(file.ListItemAllFields); context.Load(context.BaseList.Fields); context.ExecuteQuery(); foreach (Field field in spfields) { DCTFileField fileField = new DCTFileField(); DCTFieldInfo fieldInfo = new DCTFieldInfo(); DCTConverterHelper.Convert(field, fieldInfo); if (string.IsNullOrEmpty(fieldInfo.ID)) { continue; } fileField.Field = fieldInfo; fileField.FieldValue = file.ListItemAllFields[field.InternalName] == null ? "" : file.ListItemAllFields[field.InternalName].ToString(); results.Add(fileField); } return(results); }
public void DCMAddUserRole(int storageObjID, string userAccount, string rolename) { (storageObjID > 0).FalseThrow <ArgumentException>("ID值{0}无效,请传入大于0的值.", storageObjID); using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { context.Load(context.Web.RoleDefinitions); context.ExecuteQuery(); RoleDefinition selectedRoleDef = null; foreach (RoleDefinition role in context.Web.RoleDefinitions) { if (role.Name == rolename) { selectedRoleDef = role; break; } } if (null == selectedRoleDef) { return; } context.Load(selectedRoleDef); ListItem listItem = GetListItemById(storageObjID, context); if (null == listItem) { return; } context.Load(listItem); context.Load(listItem.RoleAssignments); context.ExecuteQuery(); RoleAssignmentCollection roleAssignments = listItem.RoleAssignments; bool userFound = false; foreach (RoleAssignment roleAssignment in roleAssignments) { context.Load(roleAssignment.Member); context.ExecuteQuery(); if (roleAssignment.Member.LoginName != userAccount) { continue; } userFound = true; roleAssignment.RoleDefinitionBindings.Add(selectedRoleDef); roleAssignment.Update(); } if (!userFound) { RoleDefinitionBindingCollection newBinding = new RoleDefinitionBindingCollection(context); newBinding.Add(selectedRoleDef); User user = context.Web.EnsureUser(userAccount); listItem.RoleAssignments.Add(user, newBinding); listItem.Update(); } context.ExecuteQuery(); } }
public DCTFile DCMSave(DCTFolder folder, byte[] fileData, string filename, bool overwrite) { (null == folder).TrueThrow <ArgumentException>("文件夹对象为空."); (string.IsNullOrEmpty(folder.Uri) && (!folder.IsRootFolder) && folder.ID < 0).TrueThrow <ArgumentException>("文件夹无效.请提供ID或Uri"); using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { string uri = LoadUri(folder, context); using (System.IO.MemoryStream stream = new System.IO.MemoryStream(fileData)) { stream.Seek(0, System.IO.SeekOrigin.Begin); string fileUri = uri; if (!fileUri.EndsWith("/")) { fileUri += "/"; } fileUri += filename; context.ExecuteQuery(); File.SaveBinaryDirect(context, fileUri, stream, overwrite); File file = context.Web.GetFileByServerRelativeUrl(fileUri); context.Load(file); context.ExecuteQuery(); context.Load(file.Author); if (file.CheckOutType != CheckOutType.None) { context.Load(file.CheckedOutByUser); } context.Load(file.ListItemAllFields); context.Load(file.ModifiedBy); context.ExecuteQuery(); DCTFile dctFile = new DCTFile(); Uri target = new Uri(fileUri, UriKind.RelativeOrAbsolute); Uri refUri = new Uri(context.Url); dctFile.AbsoluteUri = target.MakeAbsolute(refUri).ToString(); UriHelper.CombinePath(context.Url, fileUri); DCTConverterHelper.Convert(file, dctFile); return(dctFile); } } }
private static List <Field> GetSPFields(BaseCollection <DCTFieldInfo> fields, DocLibContext context) { (null == fields || fields.Count == 0).TrueThrow <ArgumentException>("Field集合为空或元素个数为0"); List <Field> results = new List <Field>(); foreach (DCTFieldInfo field in fields) { Field spfield = null; if (!string.IsNullOrEmpty(field.Title)) { spfield = context.BaseList.Fields.GetByTitle(field.Title); } else if ((!string.IsNullOrEmpty(field.InternalName))) { spfield = context.BaseList.Fields.GetByInternalNameOrTitle(field.InternalName); } else if ((!string.IsNullOrEmpty(field.ID))) { spfield = context.BaseList.Fields.GetById(new Guid(field.ID)); } context.Load(spfield); results.Add(spfield); } context.ExecuteQuery(); return(results); }
private RoleDefinitionBindingCollection BuildRoleDefninitionBinding(DocLibContext context, DCTRoleAssignment dctRoleAssignment, out bool ignore) { RoleDefinitionBindingCollection binding = new RoleDefinitionBindingCollection(context); int count = 0; foreach (DCTRoleDefinition dctRoleDefinition in dctRoleAssignment.RoleDefinitions) { if (dctRoleDefinition.Name == IgnoreRole) { continue; } RoleDefinition spRoleDefinition = null; if (!string.IsNullOrEmpty(dctRoleDefinition.Name)) { spRoleDefinition = context.Web.RoleDefinitions.GetByName(dctRoleDefinition.Name); } else { spRoleDefinition = context.Web.RoleDefinitions.GetById(dctRoleDefinition.ID); } context.Load(spRoleDefinition); context.ExecuteQuery(); binding.Add(spRoleDefinition); count++; } ignore = (count == 0); return(binding); }
private string LoadUri(DCTStorageObject folder, DocLibContext context) { if (folder is DCTFolder) { if ((folder as DCTFolder).IsRootFolder) { context.Load(context.BaseList.RootFolder); context.ExecuteQuery(); return(context.BaseList.RootFolder.ServerRelativeUrl); } } if (folder.Uri.IsNotEmpty()) { return(folder.Uri); } ListItem listItem = GetListItemById(folder.ID, context); (listItem == null).TrueThrow <TargetNotFoundException>("文件或文件夹(ID={0})不存在。", folder.ID); return(listItem["FileRef"].ToString()); }
private void PreCheckFields(BaseCollection <DCTFileField> fileFields, DocLibContext context) { (null == fileFields || fileFields.Count == 0).TrueThrow <ArgumentException>("Field集合为空或元素个数为0"); foreach (DCTFileField fileField in fileFields) { (null == fileField.Field).TrueThrow <ArgumentException>("Field元素为空。"); (string.IsNullOrEmpty(fileField.Field.Title) && string.IsNullOrEmpty(fileField.Field.InternalName) && string.IsNullOrEmpty(fileField.Field.ID)).TrueThrow <ArgumentException>("要设置文件夹的Field,请提供ID、Internal Name或Title."); if (string.IsNullOrEmpty(fileField.Field.InternalName)) { Field field = null; if (!string.IsNullOrEmpty(fileField.Field.ID)) { field = context.BaseList.Fields.GetById(new Guid(fileField.Field.ID)); } else if ((!string.IsNullOrEmpty(fileField.Field.Title))) { field = context.BaseList.Fields.GetByTitle(fileField.Field.Title); } context.Load(field); context.ExecuteQuery(); fileField.Field.InternalName = field.InternalName; } } }
public void DCMSetRoleAssignments(int storageObjID, BaseCollection <DCTRoleAssignment> roleAssignments) { (storageObjID > 0).FalseThrow <ArgumentException>("ID值{0}无效,请传入大于0的值.", storageObjID); using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { ListItem listItem = GetListItemById(storageObjID, context); if (null == listItem) { return; } context.Load(listItem); listItem.BreakRoleInheritance(true, true); context.Load(listItem.RoleAssignments); context.ExecuteQuery(); foreach (RoleAssignment roleAssignment in listItem.RoleAssignments) { roleAssignment.RoleDefinitionBindings.RemoveAll(); roleAssignment.Update(); } context.ExecuteQuery(); foreach (DCTRoleAssignment dctRoleAssignment in roleAssignments) { bool ignore = false; RoleDefinitionBindingCollection binding = BuildRoleDefninitionBinding(context, dctRoleAssignment, out ignore); Principal principal = BuildSharepointPrincipal(context, dctRoleAssignment); if (ignore) { continue; } listItem.RoleAssignments.Add(principal, binding); listItem.Update(); } context.ExecuteQuery(); } }
public BaseCollection<DCTFile> DCMQueryDocByField(BaseCollection<DCTFileField> fields) { BaseCollection<DCTFile> files = new BaseCollection<DCTFile>(); using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { CamlQuery query = new CamlQuery(); CamlExpression caml = null; foreach (DCTFileField item in fields) { string valueText = string.Empty; switch (item.Field.ValueType) { case DCTFieldType.Boolean: break; case DCTFieldType.DateTime: break; case DCTFieldType.Decimal: break; case DCTFieldType.Integer: valueText = "Counter"; break; case DCTFieldType.Note: break; case DCTFieldType.Text: valueText = "Text"; break; case DCTFieldType.User: break; default: break; } if (caml == null) { caml = Caml.Field(item.Field.InternalName).Eq(Caml.Value(valueText, item.FieldValue)); } else { caml = caml.And(Caml.Field(item.Field.InternalName).Eq(Caml.Value(valueText, item.FieldValue))); } } query.ViewXml = Caml.SimpleView(caml, CamlBuilder.ViewType.RecursiveAll).ToCamlString(); ListItemCollection items = clientContext.BaseList.GetItems(query); clientContext.Load(items); clientContext.ExecuteQuery(); foreach (ListItem item in items) { DCTFile file = new DCTFile(); DCTConverterHelper.Convert(item, file); files.Add(file); } } return files; }
public DCTFolder DCMGetFolderByUri(string uri) { using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { Folder folder = clientContext.Web.GetFolderByServerRelativeUrl(uri); clientContext.Load(folder); CamlQuery query = new CamlQuery(); query.ViewXml = Caml.SimpleView(Caml.Field("FileRef").Eq(Caml.Value("Text", uri)), CamlBuilder.ViewType.RecursiveAll).ToCamlString(); ListItemCollection items = clientContext.BaseList.GetItems(query); clientContext.Load(items); DCTFolder result; try { clientContext.ExecuteQuery(); if (string.Compare(folder.ServerRelativeUrl, this.DCMGetRootFolder().Uri, true) == 0) { result = new DCTFolder() { ID = 0, IsRootFolder = true } } ; else { result = new DCTFolder() { ID = items[0].Id, IsRootFolder = false } }; DCTConverterHelper.Convert(folder, result); } catch { result = null; } return(result); } }
public BaseCollection <DCTRoleAssignment> DCMGetRoleAssignments(int storageObjID) { (storageObjID <= 0).TrueThrow <ArgumentException>("ID:{0}无效,请传入大于0的值.", storageObjID); using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { ListItem listItem = GetListItemById(storageObjID, context); if (null == listItem) { return(new BaseCollection <DCTRoleAssignment>()); } context.Load(listItem); context.Load(listItem.RoleAssignments); context.ExecuteQuery(); RoleAssignmentCollection roleAssignments = listItem.RoleAssignments; BaseCollection <DCTRoleAssignment> results = new BaseCollection <DCTRoleAssignment>(); foreach (RoleAssignment roleAssignment in roleAssignments) { DCTRoleAssignment dctRoleAssignment = new DCTRoleAssignment(); context.Load(roleAssignment.Member); context.Load(roleAssignment.RoleDefinitionBindings); context.ExecuteQuery(); dctRoleAssignment.Member = GetPrinciple(roleAssignment.Member.PrincipalType); DCTConverterHelper.Convert(roleAssignment.Member, dctRoleAssignment.Member); dctRoleAssignment.RoleDefinitions = new BaseCollection <DCTRoleDefinition>(); RoleDefinitionBindingCollection bindingCollection = roleAssignment.RoleDefinitionBindings; foreach (RoleDefinition roleDefinition in bindingCollection) { DCTRoleDefinition dctRoleDefinition = new DCTRoleDefinition(); DCTConverterHelper.Convert(roleDefinition, dctRoleDefinition); dctRoleAssignment.RoleDefinitions.Add(dctRoleDefinition); } results.Add(dctRoleAssignment); } return(results); } }
public DCTFile DCMGetFileByUri(string uri) { using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { File file = context.Web.GetFileByServerRelativeUrl(uri); context.Load(file.ListItemAllFields); context.ExecuteQuery(); DCTFile dctFile = new DCTFile(); DCTConverterHelper.Convert(file.ListItemAllFields, dctFile); return(dctFile); } }
public void DCMRemoveUserRoles(int storageObjID, string userAccount, BaseCollection <string> rolesToRemove) { (storageObjID > 0).FalseThrow <ArgumentException>("ID值{0}无效,请传入大于0的值.", storageObjID); using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { ListItem listItem = GetListItemById(storageObjID, context); if (null == listItem) { return; } context.Load(listItem); context.Load(listItem.RoleAssignments); context.ExecuteQuery(); RoleAssignmentCollection roleAssignments = listItem.RoleAssignments; foreach (RoleAssignment roleAssignment in roleAssignments) { context.Load(roleAssignment.Member); context.Load(roleAssignment.RoleDefinitionBindings); context.ExecuteQuery(); if (!IsSameUser(roleAssignment.Member.LoginName, userAccount)) { continue; } for (int i = 0; i < roleAssignment.RoleDefinitionBindings.Count; i++) { RoleDefinition roleDefinition = roleAssignment.RoleDefinitionBindings[i]; string rolename = roleDefinition.Name; if (rolesToRemove.Contains(rolename)) { roleAssignment.RoleDefinitionBindings.Remove(roleDefinition); i--; } } roleAssignment.Update(); } context.ExecuteQuery(); } }
public DCTRoleDefinition DCMCreateRole(string rolename, string descriptioin) { using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { RoleDefinitionCreationInformation roleCreationInfo = new RoleDefinitionCreationInformation() { BasePermissions = new BasePermissions(), Name = rolename, Description = descriptioin }; context.Web.RoleDefinitions.Add(roleCreationInfo); context.Load(context.Web.RoleDefinitions); context.ExecuteQuery(); RoleDefinition roleDefinition = context.Web.RoleDefinitions.GetByName(rolename); context.Load(roleDefinition); context.ExecuteQuery(); DCTRoleDefinition dctRoleDefinition = new DCTRoleDefinition(); DCTConverterHelper.Convert(roleDefinition, dctRoleDefinition); return(dctRoleDefinition); } }
public DCTFolder DCMGetParentFolder(DCTStorageObject storageObject) { using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { ListItem listItem = LoadItem(storageObject, clientContext); (listItem == null).TrueThrow <ArgumentException>("无法找到文件夹,传入的为根目录或文件、文件夹不存在。"); DCTFolder folder = null; clientContext.Load(clientContext.BaseList.RootFolder); clientContext.ExecuteQuery(); if (string.Compare(listItem.FieldValues["FileDirRef"].ToString(), clientContext.BaseList.RootFolder.ServerRelativeUrl, true) == 0) { folder = new DCTFolder() { ID = 0, IsRootFolder = true }; DCTConverterHelper.Convert(clientContext.BaseList.RootFolder, folder); } else { CamlQuery query = new CamlQuery(); query.ViewXml = Caml.SimpleView(Caml.Field("ServerUrl").Eq(Caml.Value("Text", listItem.FieldValues["FileDirRef"].ToString())), CamlBuilder.ViewType.RecursiveAll).ToCamlString(); ListItemCollection parentListItem = clientContext.BaseList.GetItems(query); clientContext.Load(parentListItem); clientContext.ExecuteQuery(); folder = new DCTFolder(); DCTConverterHelper.Convert(parentListItem[0], folder); } return(folder); } }
public void DCMDeleteField(DCTFieldInfo field) { (string.IsNullOrEmpty(field.InternalName) && string.IsNullOrEmpty(field.Title) && string.IsNullOrEmpty(field.ID)).TrueThrow <ArgumentException>("请设置Field的Title、Internal Name或ID."); using (DocLibContext context = new DocLibContext()) { Field spField = null; if (!string.IsNullOrEmpty(field.Title)) { spField = context.BaseList.Fields.GetByTitle(field.Title); } else if (!string.IsNullOrEmpty(field.InternalName)) { spField = context.BaseList.Fields.GetByInternalNameOrTitle(field.InternalName); } else if (!string.IsNullOrEmpty(field.ID)) { spField = context.BaseList.Fields.GetById(new Guid(field.ID)); } else { throw new TargetNotFoundException(); } context.Load(spField); context.ExecuteQuery(); spField.DeleteObject(); context.BaseList.Update(); context.Load(spField); context.ExecuteQuery(); //context.BaseList.Fields. } }
public BaseCollection <DCTFileVersion> DCMGetVersions(int fileId) { using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { File file = getFileById(fileId, context); context.Load(file.Versions); context.ExecuteQuery(); BaseCollection <DCTFileVersion> results = new BaseCollection <DCTFileVersion>(); foreach (FileVersion version in file.Versions) { context.Load(version.CreatedBy); context.ExecuteQuery(); DCTFileVersion dctFileVersion = new DCTFileVersion(); dctFileVersion.AbsoluteUri = UriHelper.CombinePath(context.Url, version.Url); DCTConverterHelper.Convert(version, dctFileVersion); results.Add(dctFileVersion); } return(results); } }
public BaseCollection<DCTFileVersion> DCMGetVersions(int fileId) { using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { File file = getFileById(fileId, context); context.Load(file.Versions); context.ExecuteQuery(); BaseCollection<DCTFileVersion> results = new BaseCollection<DCTFileVersion>(); foreach (FileVersion version in file.Versions) { context.Load(version.CreatedBy); context.ExecuteQuery(); DCTFileVersion dctFileVersion = new DCTFileVersion(); dctFileVersion.AbsoluteUri = UriHelper.CombinePath(context.Url, version.Url); DCTConverterHelper.Convert(version, dctFileVersion); results.Add(dctFileVersion); } return results; } }
private static ListItem GetListItemById(int id, DocLibContext clientContext) { List list = clientContext.BaseList; CamlQuery query = new CamlQuery(); Caml caml = Caml.SimpleView(Caml.Field("ID").Eq(Caml.Value("Counter", id.ToString())), CamlBuilder.ViewType.RecursiveAll); query.ViewXml = caml.ToCamlString(); ListItemCollection items = list.GetItems(query); clientContext.Load(items); clientContext.ExecuteQuery(); if (items.Count == 0) return null; return items[0]; }
public DCTFolder DCMGetRootFolder() { using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { Folder rootFolder = clientContext.BaseList.RootFolder; clientContext.Load(rootFolder); clientContext.ExecuteQuery(); DCTFolder result = new DCTFolder() { ID = 0, IsRootFolder = true }; DCTConverterHelper.Convert(rootFolder, result); return result; } }
private bool FieldExists(DCTFieldInfo field, DocLibContext context) { context.Load(context.BaseList.Fields); context.ExecuteQuery(); bool found = false; foreach (Field spField in context.BaseList.Fields) { if (spField.Title == field.Title) { found = true; break; } } return(found); }
public BaseCollection <DCTPermission> DCMGetRolePermissions(DCTRoleDefinition role) { (role.ID <= 0 && string.IsNullOrEmpty(role.Name)).TrueThrow <ArgumentException>("角色中必须包含名称或ID."); using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { RoleDefinition roleDef = null; if (!string.IsNullOrEmpty(role.Name)) { roleDef = context.Web.RoleDefinitions.GetByName(role.Name); } else { roleDef = context.Web.RoleDefinitions.GetById(role.ID); } context.Load(roleDef); context.ExecuteQuery(); BaseCollection <DCTPermission> permissions = new BaseCollection <DCTPermission>(); if (roleDef.BasePermissions.Has(PermissionKind.ViewListItems)) { permissions.Add(DCTPermission.ViewFileOrFolder); } if (roleDef.BasePermissions.Has(PermissionKind.AddListItems)) { permissions.Add(DCTPermission.AddFileOrFolder); } if (roleDef.BasePermissions.Has(PermissionKind.EditListItems)) { permissions.Add(DCTPermission.UpdateFileOrFolder); } if (roleDef.BasePermissions.Has(PermissionKind.DeleteListItems)) { permissions.Add(DCTPermission.DeleteFileOrFolder); } return(permissions); } }
private ListItem GetListItemByUri(string uri, DocLibContext context) { CamlQuery query = new CamlQuery(); query.ViewXml = Caml.SimpleView(Caml.Field("FileRef").Eq(Caml.Value("Text", uri)), CamlBuilder.ViewType.RecursiveAll).ToCamlString(); ListItemCollection itemCollection = context.BaseList.GetItems(query); context.Load(itemCollection); context.ExecuteQuery(); if (itemCollection.Count == 0) { return(null); } return(itemCollection[0]); }
private byte[] getVersionContetById(File file, int versionId, DocLibContext context) { context.Load(file.Versions); context.ExecuteQuery(); FileVersion selectedVersion = null; foreach (FileVersion version in file.Versions) { if (version.ID == versionId) { selectedVersion = version; break; } } (null == selectedVersion).TrueThrow <TargetNotFoundException>("版本(ID={0})未找到。", versionId); return(context.OpenBinary(selectedVersion.Url)); }
private static ListItem GetListItemById(int id, DocLibContext clientContext) { List list = clientContext.BaseList; CamlQuery query = new CamlQuery(); Caml caml = Caml.SimpleView(Caml.Field("ID").Eq(Caml.Value("Counter", id.ToString())), CamlBuilder.ViewType.RecursiveAll); query.ViewXml = caml.ToCamlString(); ListItemCollection items = list.GetItems(query); clientContext.Load(items); clientContext.ExecuteQuery(); if (items.Count == 0) { return(null); } return(items[0]); }
public DCTFolder DCMGetRootFolder() { using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { Folder rootFolder = clientContext.BaseList.RootFolder; clientContext.Load(rootFolder); clientContext.ExecuteQuery(); DCTFolder result = new DCTFolder() { ID = 0, IsRootFolder = true }; DCTConverterHelper.Convert(rootFolder, result); return(result); } }
private ListItem LoadItem(DCTStorageObject folder, DocLibContext context) { if (folder is DCTFolder) { if ((folder as DCTFolder).IsRootFolder) { context.Load(context.BaseList.RootFolder); context.ExecuteQuery(); return(null); } } if (folder.Uri.IsNotEmpty()) { return(GetListItemByUri(folder.Uri, context)); } return(GetListItemById(folder.ID, context)); }
public BaseCollection <DCTFile> DCMQueryDocByCaml(string camlText) { BaseCollection <DCTFile> files = new BaseCollection <DCTFile>(); using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { CamlQuery query = new CamlQuery(); query.ViewXml = camlText; ListItemCollection items = clientContext.BaseList.GetItems(query); clientContext.Load(items); clientContext.ExecuteQuery(); foreach (ListItem item in items) { DCTFile file = new DCTFile(); DCTConverterHelper.Convert(item, file); files.Add(file); } } return(files); }
public void DCMSetFileFields(int fileId, BaseCollection <DCTFileField> fileFields) { using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { PreCheckFields(fileFields, context); string uri = LoadUri(new DCTFile() { ID = fileId }, context); File file = context.Web.GetFileByServerRelativeUrl(uri); context.Load(file.ListItemAllFields); foreach (DCTFileField fileField in fileFields) { file.ListItemAllFields[fileField.Field.InternalName] = fileField.FieldValue; } file.ListItemAllFields.Update(); context.ExecuteQuery(); } }
public void DCMSetRolePermissions(DCTRoleDefinition role, BaseCollection <DCTPermission> permissions) { (role.ID <= 0 && string.IsNullOrEmpty(role.Name)).TrueThrow <ArgumentException>("角色中必须包含名称或ID."); if (permissions == null || permissions.Count == 0) { return; } using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { RoleDefinition roleDef = null; if (!string.IsNullOrEmpty(role.Name)) { roleDef = context.Web.RoleDefinitions.GetByName(role.Name); } else { roleDef = context.Web.RoleDefinitions.GetById(role.ID); } context.Load(roleDef); context.ExecuteQuery(); BasePermissions basePerm = new BasePermissions(); foreach (DCTPermission permission in permissions) { basePerm.Set((PermissionKind)permission); } roleDef.BasePermissions = basePerm; roleDef.Update(); context.ExecuteQuery(); } }
public BaseCollection <DCTFieldInfo> DCMGetFields() { using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { context.Load(context.BaseList.Fields); context.ExecuteQuery(); BaseCollection <DCTFieldInfo> result = new BaseCollection <DCTFieldInfo>(); foreach (Field field in context.BaseList.Fields) { DCTFieldType fieldType; if (!Enum.TryParse <DCTFieldType>(field.TypeAsString, out fieldType)) { continue; } DCTFieldInfo fieldInfo = new DCTFieldInfo(); DCTConverterHelper.Convert(field, fieldInfo); result.Add(fieldInfo); } return(result); } }
public DCTFieldInfo DCMAddField(MCS.Library.SOA.DocServiceContract.DCTFieldInfo field, bool raiseErrorWhenExist) { ((field.Title).IsNullOrEmpty()).TrueThrow <ArgumentException>("请设置Field的Title."); using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { if (FieldExists(field, context) && raiseErrorWhenExist) { throw new FieldAlreadyExistException(); } Field spField = context.BaseList.Fields.AddFieldAsXml("<Field Type=\"" + field.ValueType.ToString() + "\" DisplayName=\"" + field.Title + "\" Name=\"" + field.Title + "\"/>", true, AddFieldOptions.AddFieldToDefaultView); DCTConverterHelper.Convert(field, spField); context.Load(spField); context.ExecuteQuery(); field.ID = spField.Id.ToString(); field.InternalName = spField.InternalName; return(field); } }
public DCTFolder DCMCreateFolder(string name, DCTFolder parentFolder) { DCTFolder folder = new DCTFolder(); using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { Folder cOMParentFolder = clientContext.Web.GetFolderByServerRelativeUrl(LoadUri(parentFolder, clientContext)); if (cOMParentFolder != null) { cOMParentFolder.Folders.Add(UriHelper.CombinePath(parentFolder.Uri, name)); } clientContext.ExecuteQuery(); CamlQuery query = new CamlQuery() { FolderServerRelativeUrl = parentFolder.Uri }; query.ViewXml = Caml.SimpleView(Caml.Field("ServerUrl").Eq(Caml.Value("Text", (parentFolder.Uri[parentFolder.Uri.Length - 1] != '/' ? parentFolder.Uri + "/" : parentFolder.Uri) + name)), CamlBuilder.ViewType.Default).ToCamlString(); ListItemCollection items = clientContext.BaseList.GetItems(query); clientContext.Load(items); clientContext.ExecuteQuery(); if (items.Count != 0) { folder.AbsoluteUri = UriHelper.CombinePath(parentFolder.AbsoluteUri, name); folder.Name = name; folder.Uri = UriHelper.CombinePath(parentFolder.Uri, name); folder.IsRootFolder = false; folder.ID = items[0].Id; } } return(folder); }
private byte[] getVersionContetById(File file, int versionId, DocLibContext context) { context.Load(file.Versions); context.ExecuteQuery(); FileVersion selectedVersion = null; foreach (FileVersion version in file.Versions) { if (version.ID == versionId) { selectedVersion = version; break; } } (null == selectedVersion).TrueThrow<TargetNotFoundException>("版本(ID={0})未找到。", versionId); return context.OpenBinary(selectedVersion.Url); }
public BaseCollection<DCTFile> DCMQueryDocByCaml(string camlText) { BaseCollection<DCTFile> files = new BaseCollection<DCTFile>(); using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { CamlQuery query = new CamlQuery(); query.ViewXml = camlText; ListItemCollection items = clientContext.BaseList.GetItems(query); clientContext.Load(items); clientContext.ExecuteQuery(); foreach (ListItem item in items) { DCTFile file = new DCTFile(); DCTConverterHelper.Convert(item, file); files.Add(file); } } return files; }
public DCTFolder DCMGetParentFolder(DCTStorageObject storageObject) { using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { ListItem listItem = LoadItem(storageObject, clientContext); (listItem == null).TrueThrow<ArgumentException>("无法找到文件夹,传入的为根目录或文件、文件夹不存在。"); DCTFolder folder = null; clientContext.Load(clientContext.BaseList.RootFolder); clientContext.ExecuteQuery(); if (string.Compare(listItem.FieldValues["FileDirRef"].ToString(), clientContext.BaseList.RootFolder.ServerRelativeUrl, true) == 0) { folder = new DCTFolder() { ID = 0, IsRootFolder = true }; DCTConverterHelper.Convert(clientContext.BaseList.RootFolder, folder); } else { CamlQuery query = new CamlQuery(); query.ViewXml = Caml.SimpleView(Caml.Field("ServerUrl").Eq(Caml.Value("Text", listItem.FieldValues["FileDirRef"].ToString())), CamlBuilder.ViewType.RecursiveAll).ToCamlString(); ListItemCollection parentListItem = clientContext.BaseList.GetItems(query); clientContext.Load(parentListItem); clientContext.ExecuteQuery(); folder = new DCTFolder(); DCTConverterHelper.Convert(parentListItem[0], folder); } return folder; } }
public DCTFolder DCMCreateFolder(string name, DCTFolder parentFolder) { DCTFolder folder = new DCTFolder(); using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { Folder cOMParentFolder = clientContext.Web.GetFolderByServerRelativeUrl(LoadUri(parentFolder, clientContext)); if (cOMParentFolder != null) { cOMParentFolder.Folders.Add(UriHelper.CombinePath(parentFolder.Uri, name)); } clientContext.ExecuteQuery(); CamlQuery query = new CamlQuery() { FolderServerRelativeUrl = parentFolder.Uri }; query.ViewXml = Caml.SimpleView(Caml.Field("ServerUrl").Eq(Caml.Value("Text", (parentFolder.Uri[parentFolder.Uri.Length - 1] != '/' ? parentFolder.Uri + "/" : parentFolder.Uri) + name)), CamlBuilder.ViewType.Default).ToCamlString(); ListItemCollection items = clientContext.BaseList.GetItems(query); clientContext.Load(items); clientContext.ExecuteQuery(); if (items.Count != 0) { folder.AbsoluteUri = UriHelper.CombinePath(parentFolder.AbsoluteUri, name); folder.Name = name; folder.Uri = UriHelper.CombinePath(parentFolder.Uri, name); folder.IsRootFolder = false; folder.ID = items[0].Id; } } return folder; }
public BaseCollection<DCTStorageObject> DCMGetChildren(DCTFolder folder, DCTContentType contentType) { BaseCollection<DCTStorageObject> childs = new BaseCollection<DCTStorageObject>(); BaseCollection<DCTFile> childFiles = new BaseCollection<DCTFile>(); BaseCollection<DCTFolder> childFolders = new BaseCollection<DCTFolder>(); using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName())) { Folder clientOMFolder = clientContext.Web.GetFolderByServerRelativeUrl(folder.Uri); CamlQuery query = new CamlQuery(); query.FolderServerRelativeUrl = LoadUri(folder, clientContext); query.ViewXml = Caml.SimpleView(Caml.EmptyCaml(), CamlBuilder.ViewType.Default).ToCamlString(); ListItemCollection items = clientContext.BaseList.GetItems(query); clientContext.Load(items); clientContext.ExecuteQuery(); foreach (ListItem item in items) { switch (item.FileSystemObjectType) { case FileSystemObjectType.File: if ((contentType & DCTContentType.File) != DCTContentType.None) { DCTFile itemFile = new DCTFile(); DCTConverterHelper.Convert(item, itemFile); childs.Add(itemFile); //childFiles.Add(itemFile); } break; case FileSystemObjectType.Folder: if ((contentType & DCTContentType.Folder) != DCTContentType.None) { DCTFolder itemFolder = new DCTFolder(); DCTConverterHelper.Convert(item, itemFolder); childs.Add(itemFolder); //childFolders.Add(itemFolder); } break; case FileSystemObjectType.Invalid: break; case FileSystemObjectType.Web: break; default: break; } } } foreach (DCTFolder item in childFolders) childs.Add(item); foreach (DCTFile item in childFiles) childs.Add(item); return childs; }