防止一组用户使用csom访问共享文档

时间:2019-11-06 08:38:58

标签: sharepoint csom

我想阻止共享点(例如:成员)中的一组用户使用csom访问“共享文档”。 但是,当我执行我的代码时,它不起作用(我上面提到的组中的帐户仍然可以在我的站点中访问。 有我的代码:

ClientContext context = new ClientContext("https://example.com/sites/Litware");
            SecureString password = new SecureString();
            foreach (char c in "abcd".ToCharArray()) password.AppendChar(c);
            context.Credentials = new SharePointOnlineCredentials("example@example.onmicrosoft.com", password);
            var web = context.Web;
            context.Load(web);
            context.ExecuteQuery();
            var memGroup = web.SiteGroups.GetByName("Member");
            context.Load(memGroup);            
            var users = memGroup.Users;
            context.Load(users);
            context.ExecuteQuery();
            foreach (var user in users)
            {
                Principal principal = web.EnsureUser(user.LoginName);
                var folder = web.GetFolderByServerRelativeUrl("/Shared Documents");
                var roleDef = context.Site.RootWeb.RoleDefinitions.GetByType(RoleType.None);                               
                var roleBindings = new RoleDefinitionBindingCollection(context) { roleDef };
                folder.ListItemAllFields.BreakRoleInheritance(true, false);
                folder.ListItemAllFields.RoleAssignments.Add(principal, roleBindings);
            }

那么,我在哪里弄错了以及如何解决呢?

1 个答案:

答案 0 :(得分:0)

请参考以下代码:

        /// <summary>
        /// Edit the position of content in all pages in a document
        /// </summary>
        /// <param name="file">Path of the file to be edited</param>
        /// <param name="saveAs">Path of the file to be saved</param>
        /// <param name="x">Top left corner - X</param>
        /// <param name="y">Top left corner - Y/param>
        /// <param name="w">Rectangle width</param>
        /// <param name="h">Rectangle height</param>
        /// <param name="moveX">Move X (+ right, - left)</param>
        /// <param name="moveY">Move Y (+ down, - up)</param>
        [Description("Edit the scale of all pages in a document")]
        [CommandLineVisible(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Order("EditPosition.00")]
        public static void EditPosition(
            [Description("Path of the file to be edited")]
            [Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
            string file,
            [Description("Path of the file to be saved")]
            [Editor(typeof(System.Windows.Forms.Design.FileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
            string saveAs,
            [Description("Top left corner - X")]
            float x,
            [Description("Top left corner - Y")]
            float y,
            [Description("Rectangle width")]
            float w,
            [Description("Rectangle height")]
            float h,
            [Description("Move X (+ right, - left)")]
            float moveX,
            [Description("Move Y (+ down, - up)")]
            float moveY
            )
        {
            file = prepareInput(file);
            FileInfo input = new FileInfo(file);
            writeInfoLine(String.Format("Datei \"{0}\"", file));
            FileInfo tmpPDF = new FileInfo(System.IO.Path.GetTempFileName());
            if (saveAs == String.Empty || saveAs == null)
            {
                saveAs = input.FullName;
            }

            using (var reader = new PdfReader(new MemoryStream(File.ReadAllBytes(input.FullName))))
            using (var document = new Document(reader.GetPageSize(1)))
            using (var ms = new MemoryStream())
            using (var writer = PdfWriter.GetInstance(document, ms))
            {
                document.Open();
                PdfContentByte cb = writer.DirectContent;

                // Rectangle umrechnen; PDF Orientierung von unten links
                RectangleF area = new RectangleF(x, y, w, h);

                for (int p = 1; p <= reader.NumberOfPages; p++)
                {
                    PdfImportedPage page = writer.GetImportedPage(reader, p);
                    iTextSharp.text.Rectangle pageSize = reader.GetPageSize(p);


                    document.SetPageSize(pageSize);
                    document.NewPage();

                    PdfTemplate template1 = cb.CreateTemplate(pageSize.Width, pageSize.Height);
                    template1.Rectangle(0, 0, pageSize.Width, pageSize.Height);
                    template1.Rectangle(area.Left, pageSize.Height - area.Bottom,
                            area.Width, area.Height);
                    template1.EoClip(); // ADD 1
                    template1.NewPath();
                    template1.AddTemplate(page, 0, 0);
                    PdfTemplate template2 = cb.CreateTemplate(pageSize.Width, pageSize.Height);
                    template2.Rectangle(area.Left, pageSize.Height - area.Bottom,
                            area.Width, area.Height);
                    template2.Clip(); // ADD 2
                    template2.NewPath();
                    template2.AddTemplate(page, 0, 0);
                    cb.AddTemplate(template1, 0, 0);
                    cb.AddTemplate(template2, moveX, -moveY);

                }
                document.Close();
                File.WriteAllBytes(tmpPDF.FullName, ms.GetBuffer());
            }

            // Move temp.pdf to destination
            lastpath = saveOutput(saveAs, tmpPDF, input, null);
            return;
        }

参考:

Remove specific user permissions from one folder in Sharepoint online with powershell