コード例 #1
0
        //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);
            }
        }
コード例 #2
0
        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);
        }
コード例 #3
0
 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);
     }
 }
コード例 #4
0
        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);
                }
            }
        }
コード例 #5
0
        public DCTFolder DCMGetFolderById(int id)
        {
            using (DocLibContext clientContext = new DocLibContext(ServiceHelper.GetDocumentLibraryName()))
            {
                if (id == 0)
                {
                    return(DCMGetRootFolder());
                }
                ListItem item = GetListItemById(id, clientContext);

                (item == null).TrueThrow <TargetNotFoundException>("无法根据ID:{0}找到对应的目录。", id);
                DCTFolder folder = new DCTFolder();
                DCTConverterHelper.Convert(item, folder);

                return(folder);
            }
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
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);
            }
        }
コード例 #8
0
        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);
            }
        }
コード例 #9
0
        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);
        }
コード例 #10
0
        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);
            }
        }
コード例 #11
0
        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);
            }
        }
コード例 #12
0
        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);
            }
        }
コード例 #13
0
        public DCTFile DCMGetFileInFolder(DCTFolder folder, string filename)
        {
            DCTFolder curFolder = null;

            if (!folder.Uri.IsNullOrEmpty())
            {
                curFolder = DCMGetFolderByUri(folder.Uri);
            }
            else
            {
                curFolder = DCMGetFolderById(folder.ID);
            }

            string uri     = folder.Uri;
            string fileUri = UriHelper.CombinePath(uri, filename);

            using (DocLibContext context = new DocLibContext(ServiceHelper.GetDocumentLibraryName()))
            {
                ListItem item    = GetListItemByUri(fileUri, context);
                DCTFile  dctFile = new DCTFile();
                DCTConverterHelper.Convert(item, dctFile);
                return(dctFile);
            }
        }
コード例 #14
0
        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);
            }
        }
コード例 #15
0
        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);
            }
        }
コード例 #16
0
        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);
        }
コード例 #17
0
        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);
        }