以编程方式对上传的共享点文档应用权限

发布于 2024-11-07 15:36:04 字数 3278 浏览 0 评论 0原文

我正在开发一个接口来从共享点检索文件。我有以下方法来上传文档:

    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

  • 是否可以在上传过程中设置权限?
  • 我的理解是,当文件上传时,它会继承上传到的列表的权限。
  • 我当前的下载方法返回一个 ListItem。是否可以将 ListItem 转换为 SPListItem?返回 SpListItem 的原因是由于我上面发布的博客.. 看来 SPListItem 包含我需要的权限信息 [打破并应用新权限]。

我的下载方法返回一个 ListItem..我怎样才能将其作为 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;
    }

提前致谢

i'm developing an interface to retrieve a file from sharepoint. i have the following method to upload a document:

    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;
    }

However, my next step is to apply a custom permission.. i've found some blogs online which demonstrate how to do this:
http://howtosharepoint.blogspot.com/2010/10/programmatically-add-delete-modify-item.html

  • Is it possible to set the permission in the uploading process?
  • my understanding is, when the file is uploaded it inherits permissions from the list its uploaded to.
  • my current download method returns a ListItem.. Is it possible to cast a ListItem to SPListItem? The reason for returning a SpListItem is due to the blog i posted above.. it appears SPListItem contains the permission info i need [to break and apply the new permission to].

my download method returns a ListItem.. how can i do this as an 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;
    }

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2024-11-14 15:36:04

看起来您正在尝试在文档库中配置项目级别权限。如果我错了请纠正我。

首先,您必须停止从父站点继承权限。然后,您可以创建一个工作流程并让它在上传文档时触发。设计一个工作流程来配置项目级别的权限比在代码中进行要容易。维护也会很容易。

Looks like you are trying to configure item level permissions in document library. Correct me if I am wrong.

Firstly, You have to stop inheriting permissions from parent site. You can then create a workflow and let it trigger when a document is uploaded. It is easy to design a workflow to configure permissions on item level than doing it in code. Maintenance would be easy too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文