zoukankan      html  css  js  c++  java
  • SharePoint 服务器端对象模型操作用户组(创建/添加/删除)

    来源于:http://www.cnblogs.com/jianyus/p/3255572.html

    摘要:几个操作SharePoint用户组的方法,已经测试通过,但是没有提升权限,如果没有权限的人操作,需要提升权限(提权代码附后)。大家需要的话,可以参考下,写在这里也给自己留个备份~~

           //创建用户组

            public static bool CreatSPGroup(string strGroupName, string strGroupDescription)

            {

                try

                {

                    using (SPSite site = new SPSite(SiteUrl))

                    {

                        using (SPWeb web = site.OpenWeb())

                        {

                            web.AllowUnsafeUpdates = true;

                            SPUser defaultUser = web.SiteUsers.GetByID(Convert.ToInt32(defaultUserID));

                            web.SiteGroups.Add(strGroupName, defaultUser, null, strGroupDescription);

                            web.AllowUnsafeUpdates = false;

                            return true;

                        }

                    }

                }

                catch

                {

                    return false;

                }

            }

            //添加用户到用户组

            public static bool AddUserToSPGroup(string strGroupName, string strLoginName, string strUserName, string strEmail, string notes)

            {

                try

                {

                    using (SPSite site = new SPSite(SiteUrl))

                    {

                        using (SPWeb web = site.OpenWeb())

                        {

                            web.AllowUnsafeUpdates = true;

                            SPGroup cGroup = web.SiteGroups.GetByName(strGroupName);

                            cGroup.AddUser(strLoginName, strEmail, strUserName, notes);

                            web.AllowUnsafeUpdates = false;

                            return true;

                        }

                    }

                }

                catch

                {

                    return false;

                }

            }

            //从用户组删除用户

            public static bool DelUserFromSPGroup(string strLoginName, string strGroupName)

            {

                try

                {

                    using (SPSite site = new SPSite(SiteUrl))

                    {

                        using (SPWeb web = site.OpenWeb())

                        {

                            web.AllowUnsafeUpdates = true;

                            SPGroup cGroup = web.SiteGroups.GetByName(strGroupName);

                            cGroup.Users.Remove(strLoginName);

                            web.AllowUnsafeUpdates = false;

                            return true;

                        }

                    }

                }

                catch

                {

                    return false;

                }

            }

    //提升权限

    SPSecurity.RunWithElevatedPrivileges (delegate()

    {

      //此处放置需要以系统账号身份运行的代码

    });

    特别注意:

    1 如果代码要操作WSS的内容,必须创建新的SPSite和SPWeb实例,利用RunWithElevatedPrivilege

    2 不能直接调用上下文对象(SPContext),上下文对象始终以当前用户身份运行

    天天想你红枣,您的健康,‘枣‘的承诺 <a href="http://shop109102900.taobao.com" target="_blank" style="color:red">天天想你红枣淘宝店</a>

  • 相关阅读:
    IDEA 学习笔记之 Scala项目开发
    IDEA 学习笔记之 Java项目开发
    IDEA 学习笔记之 安装和基本配置
    MongoDB 学习笔记之 索引
    MongoDB 学习笔记之 删除数据,集合,数据库
    Shiro学习(13)RememberMe
    Shiro学习(12)与Spring集成
    Shiro学习(11)缓存机制
    Shiro学习(10)Session管理
    Shiro学习(9)JSP标签
  • 原文地址:https://www.cnblogs.com/TNSSTAR/p/3602823.html
Copyright © 2011-2022 走看看