以编程方式将SharePoint用户/组添加到每个文档库文件夹中

本文关键字:文档 文件夹 添加 方式 编程 SharePoint 用户 | 更新日期: 2023-09-27 18:07:21

我有下面的代码,我通过谷歌搜索等各种碎片拼凑在一起。

代码像champ一样工作,提供了其中的各种库和文件夹的树状视图。

最近我们有一个重要的用户不小心从系统中删除了,当发生这种情况时,它也从每个库中删除了&文件夹就是它(就是每一个文件夹)…(我们已经破坏了每个文件夹的权限,并且没有继承库或文件夹级别的权限)

我认为这个应用程序代码递归地遍历网站上的所有库和文件夹…我只需要添加一些代码就可以将用户添加到每个文件夹。

我的问题是到目前为止我找到的每个例子/建议都有Folder.item.blahblahblah但是在我的文件夹对象

中没有名为"item"的方法

任何提示或彻底的一步一步修复我的代码下面做我需要什么?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;
using System.Net;


namespace red
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "It Begins...'r'n";
            string SitenameDev = @"https://portal/sites/devv/team";
            string SitenameProd = @"https://portal/sites/";
            ClientContext clientcontext = new ClientContext(SitenameProd);
            clientcontext.Credentials = new NetworkCredential("sitecollectionadminacct", "pswd", "Domain");

                //Load Libraries from SharePoint
                clientcontext.Load(clientcontext.Web.Lists);
                clientcontext.ExecuteQuery();
                foreach (List list in clientcontext.Web.Lists)
                {
                    try
                    {
                        if (list.BaseType.ToString() == "DocumentLibrary" && !list.IsApplicationList && list.Title != "Form Templates" && list.Title != "Customized Reports" && list.Title != "Site Collection Documents" && list.Title != "Site Collection Images" && list.Title != "Images")
                        {
                            clientcontext.Load(list);
                            clientcontext.ExecuteQuery();
                            clientcontext.Load(list.RootFolder);
                            clientcontext.Load(list.RootFolder.Folders);
                            clientcontext.Load(list.RoleAssignments);
                            clientcontext.ExecuteQuery();
                            TreeViewLibraries.ShowLines = true;
                            TreeNode LibraryNode = new TreeNode(list.Title);
                            //MessageBox.Show(LibraryNode.Name);

                            TreeViewLibraries.Nodes.Add(LibraryNode);
                            if (!list.Title.StartsWith("Nothing here"))
                            {  
                                foreach (Folder SubFolder in list.RootFolder.Folders)
                                {
                                    if (SubFolder.Name != "Forms")
                                    {
                                        TreeNode MainNode = new TreeNode(SubFolder.Name);
                                        LibraryNode.Nodes.Add(MainNode);
                                        FillTreeViewNodes(SubFolder, MainNode, clientcontext);
                                    }
                                }
                            }
                        }
                    }
                    catch  (Exception eee)
                    {
                    }
                }
        }
        //Recursive Function
        public void FillTreeViewNodes(Folder SubFolder, TreeNode MainNode, ClientContext clientcontext)
        {
            clientcontext.Load(SubFolder.Folders);
            clientcontext.ExecuteQuery();
                foreach (Folder Fol in SubFolder.Folders)
                {   
                    TreeNode SubNode = new TreeNode(Fol.Name);
                    MainNode.Nodes.Add(SubNode);
                    FillTreeViewNodes(Fol, SubNode, clientcontext);
                    //ListItem Fole = new ListItem();
                }
        }

        private void TreeViewLibraries_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            MessageBox.Show("Node: " + e.Node.Text);
            try
            {
                MessageBox.Show("Parent: " + e.Node.Parent.Text);
            }
            catch (System.NullReferenceException)
            {
                MessageBox.Show("Parent: " + "None!");
            }
        }
    }
}

以编程方式将SharePoint用户/组添加到每个文档库文件夹中

Microsoft.SharePoint.Client。ClientContext ClientContext = new Microsoft.SharePoint.Client.ClientContext("URL");

var user = (Microsoft.SharePoint.Client.Principal)clientContext.Web.EnsureUser("domain'user");

Microsoft.SharePoint.Client。CamlQuery caml = new Microsoft.SharePoint.Client.CamlQuery();

caml。ViewXml =//CAML QUERY HERE…

caml。FolderServerRelativeUrl =//这里的相对路径…

items = objList.GetItems(caml);

clientContext.Load(物品);

clientContext结构。

= new NetworkCredential(用户名,密码,域);

clientContext.ExecuteQuery ();

item = items[0];

项目。BreakRoleInheritance(真的,真的);

clientContext.ExecuteQuery ();

//你可以在这里选择评估var reader = clientContext.Web.RoleDefinitions.GetByType(Microsoft.SharePoint.Client.RoleType.Reader);

var collRdb = new Microsoft.SharePoint.Client.RoleDefinitionBindingCollection(clientContext) {reader};

item.RoleAssignments。添加(用户、collRdb);

clientContext.ExecuteQuery ();