以编程方式将权限应用于上载的 SharePoint 文档
本文关键字:上载 SharePoint 文档 应用于 权限 编程 方式 | 更新日期: 2023-09-27 17:56:27
>我正在开发一个接口来从 SharePoint 检索文件。 我有以下方法上传文档:
public bool UploadDocument(string document)
{
if (String.IsNullOrEmpty(Path.GetFullPath(document)))
{
return false;
}
var site = GetNewDataContext(SiteUrl);
try
{
using (var clientContext = GetNewContext())
{
var uploadLocation = string.Format("{0}{1}/{2}", SiteUrl, Helpers.ListNames.RequestedDocuments, Path.GetFileName(document));
//Get Document List
var documentslist = clientContext.Web.Lists.GetByTitle(Helpers.ListNames.RequestedDocuments);
var fileCreationInformation = new FileCreationInformation
{
Content = System.IO.File.ReadAllBytes(document), //Assign to content byte[] i.e. documentStream
Overwrite = true, //Allow owerwrite of document
Url = uploadLocation //Upload URL
};
var uploadFile = documentslist.RootFolder.Files.Add(fileCreationInformation);
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
site.SubmitChanges(ConflictMode.FailOnFirstConflict, true);
}
catch (Exception exception)
{
throw new ApplicationException(exception.Message);
}
return true;
}
但是,我的下一步是应用自定义权限..我在网上找到了一些博客,演示了如何做到这一点:http://howtosharepoint.blogspot.com/2010/10/programmatically-add-delete-modify-item.html
- 是否可以在上传过程中设置权限?
- 我的理解是,当文件上传时,它会从其上传到的列表中继承权限。
- 我当前的下载方法返回一个列表项。是否可以将列表项转换为 SPListItem? 返回 SpListItem 的原因是由于我上面发布的博客。SPListItem 似乎包含我需要的权限信息[以破坏和应用新权限]。
我的下载方法返回一个列表项.. 我怎样才能作为 SPListItem 执行此操作
private static ListItemCollection GetListItemCollectionFromSp(string documentListName, string name, string value, string type, int rowLimit)
{
ListItemCollection listItems = null;
using (var ctx = GetNewContext())
{
var documentsList = ctx.Web.Lists.GetByTitle(documentListName);
var camlQuery = new CamlQuery
{
ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='" + name + @"'/>
<Value Type='" + type + "'>" + value + @"</Value>
</Eq>
</Where>
<RowLimit>" + rowLimit + @"</RowLimit>
</Query>
</View>"
};
listItems = documentsList.GetItems(camlQuery);
ctx.Load(documentsList);
ctx.Load(listItems,
items => items.IncludeWithDefaultProperties(
item => item.DisplayName));
ctx.ExecuteQuery();
}
return listItems;
}
提前致谢
您似乎正在尝试在文档库中配置项目级别权限。如果我错了,请纠正我。
首先,您必须停止从父站点继承权限。然后,您可以创建一个工作流,并让它在上传文档时触发。设计工作流以在项目级别配置权限比在代码中配置更容易。维护也很容易。