0

我有下面的代码,我通过谷歌搜索等找到的各种部分拼凑在一起。

该代码像冠军一样工作,提供其中各种库和文件夹的树形视图。

最近我们意外从系统中删除了一个重要用户,当这种情况发生时,它也从每个库和文件夹中删除(这是每个单独的)..(我们已经破坏了每个文件夹的权限并且不继承库或文件夹级别的权限)

我想这个应用程序代码会递归地遍历网站上的所有库和文件夹......我可以添加一些代码来将用户添加到每个文件夹中。

我的问题是到目前为止我发现的每个示例/建议都有 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!");

            }
        }
    }
}
4

1 回答 1

0

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 查询...

caml.FolderServerRelativeUrl = //这里的相对路径...

项目 = objList.GetItems(caml);

clientContext.Load(items);

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

clientContext.ExecuteQuery();

项目=项目[0];

item.BreakRoleInheritance(true, true);

clientContext.ExecuteQuery();

// 您可以在这里选择访问权限 var reader = clientContext.Web.RoleDefinitions.GetByType(Microsoft.SharePoint.Client.RoleType.Reader);

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

item.RoleAssignments.Add(user, collRdb);

clientContext.ExecuteQuery();

于 2014-03-17T09:27:42.397 回答