博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
All Users in SharePoint Site Custom Webpart
阅读量:6906 次
发布时间:2019-06-27

本文共 3063 字,大约阅读时间需要 10 分钟。

All Users in SharePoint Site Custom Webpart

Price (USD): 0.0

Category: For e.g. WebPart, Workflow, Event Receiver etc.

This webpart will display all the users from each group in the current site. You can however add a foreach loop for each web (or site) in the site collection to get all the users in a site collection.

Our Clients is my Site name, so the groups are named as “Our Clients Visitors”, “Our Clients Members” and “Our Clients Owners”.

Steps :

1. Create a new webpart project (Side Note : I am using VseWss webpart template)

2. Add the below code in Webpart.cs file. Dont forget to rename all the webpart files before you deploy.

Code :

using System;

using System.Web.UI;

using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Collections;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace AllUsersWebPart

{
[Guid("bb4dddf0-9923-43b9-a835-210735416ddf")]
public class AllUsers : System.Web.UI.WebControls.WebParts.WebPart
{
Label lblName;
Label lblgroups;
Table tblAllUsers;

public AllUsers()

{}

protected override void CreateChildControls()

{
base.CreateChildControls();

tblAllUsers = new Table();

lblgroups = new Label();
lblName = new Label();

CreateHeaderRow(); // Will Add a header Row to the Table.

using (SPSite SPSite = new SPSite(SPContext.Current.Site.ID))

{
using (SPWeb SPWeb = SPSite.OpenWeb(SPContext.Current.Web.ID))
{

SPUserCollection AllSPWebUsers = SPContext.Current.Web.AllUsers;

SPGroupCollection AllSPWebGroups = SPContext.Current.Web.Groups;

//Iterate through each group in the current site.

foreach (SPGroup grp in AllSPWebGroups)

{
SPUserCollection UsersInGroup = grp.Users;

foreach (SPUser user in UsersInGroup)

{
lblName.Text = user.Name;

foreach (SPGroup usergrp in user.Groups)

{
lblgroups.Text = usergrp.Name;
}

AddToTable(lblName.Text, lblgroups.Text);

}
}}}

this.Controls.Add(tblAllUsers);

}
// Adding users to the Output Table.
protected void AddToTable(string UserName, string grp)
{
TableRow r = new TableRow();

TableCell CellName = new TableCell();

CellName.Text = UserName;
r.Cells.Add(CellName);

TableCell CellPermissions = new TableCell();

CellPermissions.Text = grp;
r.Cells.Add(CellPermissions);

tblAllUsers.Rows.Add(r);

}

// Create a Header Row for the Output table.

protected void CreateHeaderRow()
{

TableHeaderRow headerRow = new TableHeaderRow();

headerRow.BackColor = System.Drawing.Color.LightBlue;

TableHeaderCell headerTableCell1 = new TableHeaderCell();

TableHeaderCell headerTableCell2 = new TableHeaderCell();
headerTableCell1.Text = “User Name”;
headerTableCell1.Scope = TableHeaderScope.Column;

headerTableCell2.Text = “Group”;

headerTableCell2.Scope = TableHeaderScope.Column;

headerRow.Cells.Add(headerTableCell1);

headerRow.Cells.Add(headerTableCell2);

tblAllUsers.Rows.AddAt(0, headerRow);

}
}}

See the attached Screenshot

转载于:https://www.cnblogs.com/ahghy/archive/2013/04/10/3012238.html

你可能感兴趣的文章
Canvas-line.html
查看>>
mikadonic-nmcli 使用方法
查看>>
JavaScript
查看>>
HDU2017新生赛 友好整数
查看>>
AtCoder Regular Contest 091
查看>>
XML文件的加密与解密
查看>>
npm install出现"Unexpected end of JSON input while parsing near"
查看>>
[BZOJ3884]上帝与集合的正确用法
查看>>
20172303 2018-2019-1《程序设计与数据结构》第9周学习总结
查看>>
js循环总结
查看>>
http和https区别及概念
查看>>
Centos7上搭建activemq集群和zookeeper集群
查看>>
Java8系列之重新认识HashMap
查看>>
Oracle 12c
查看>>
tensorflow :ckpt模型转换为pytorch : hdf5模型
查看>>
详细讲解Java中方法的重载和重写
查看>>
HDU 1016 Prime Ring Problem (DFS)
查看>>
POJ 2492 A Bug's Life (并查集)
查看>>
Delete、Drop、Truncate的比较
查看>>
angular模板加载 ----ng-template
查看>>