SharePoint發行版本有SharePoint2003、SharePoint2007、Sharepoint 2010、SharePoint2013和SharePoint2016。SharePoint提供了功能強大的團隊協作環境,使得組織能夠在整個組織內部實現整合、組織、查找和提供 SharePoint站點。 幾個操作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),上下文對象始終以當前用戶身份運行 Sharepoint 可以幫助企業用戶輕松完成日常工作。 |
溫馨提示:喜歡本站的話,請收藏一下本站!