Skip to content

Instantly share code, notes, and snippets.

@meitinger
Last active December 22, 2015 09:18
Show Gist options
  • Save meitinger/6450439 to your computer and use it in GitHub Desktop.
Save meitinger/6450439 to your computer and use it in GitHub Desktop.
Updates title, department, job title, email and sip address of WSS users (and groups). Usage: Place this file under "%CommonProgramFiles%\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS" and access it via "http://<your-site-name>/_layouts/updateusers.aspx".
<%@ Page Language="C#" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Assembly Name="System.DirectoryServices.AccountManagement, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" %>
<%@ Import Namespace="System.DirectoryServices.AccountManagement" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
Copyright (C) 2011-2013, Manuel Meitinger
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<head id="Head" runat="server">
<title>Update WSS Users Information</title>
<script runat="server" type="text/C#">
private TreeNode CreateTopNode(string name)
{
TreeNode result = new TreeNode(name);
result.Expanded = false;
result.PopulateOnDemand = false;
result.SelectAction = TreeNodeSelectAction.Expand;
UsersTreeView.Nodes.Add(result);
return result;
}
private void UpdateUsers(object sender, EventArgs e)
{
UsersTreeView.Nodes.Clear();
TreeNode unknownPrincipal = CreateTopNode("Unknown Principal");
TreeNode alreadyUpToDate = CreateTopNode("Already Up-To-Date");
TreeNode updated = CreateTopNode(sender == UpdateBtn ? "Updated" : "To be updated");
SPSite site = SPContext.Current.Site;
foreach (SPListItem user in site.GetCatalog(SPListTemplateType.UserInformation).Items)
{
TreeNode node = new TreeNode(user[SPBuiltInFieldId.Name].ToString(), user.UniqueId.ToString());
node.Expanded = false;
node.PopulateOnDemand = true;
node.SelectAction = TreeNodeSelectAction.Expand;
SPPrincipalInfo info = SPUtility.ResolveWindowsPrincipal(site.WebApplication, node.Text, SPPrincipalType.SecurityGroup | SPPrincipalType.User, false);
if (info != null)
{
List<string> updates = new List<string>();
string title = info.DisplayName;
if (info.PrincipalType == SPPrincipalType.SecurityGroup)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
using (GroupPrincipal group = GroupPrincipal.FindByIdentity(context, IdentityType.SamAccountName, title))
if (group != null)
title = group.Name;
}
catch (Exception ex)
{
node.Text += string.Format(" (Error: {0})", ex.Message);
}
});
}
UpdateProperty(user, SPBuiltInFieldId.Title, title, updates);
UpdateProperty(user, SPBuiltInFieldId.Department, info.Department, updates);
UpdateProperty(user, SPBuiltInFieldId.JobTitle, info.JobTitle, updates);
UpdateProperty(user, SPBuiltInFieldId.EMail, info.Email, updates);
UpdateProperty(user, SPBuiltInFieldId.SipAddress, info.SIPAddress, updates);
if (updates.Count > 0)
{
node.Text += string.Format(" ({0})", string.Join("; ", updates.ToArray()));
updated.ChildNodes.Add(node);
if (sender == UpdateBtn)
user.Update();
}
else
alreadyUpToDate.ChildNodes.Add(node);
}
else
unknownPrincipal.ChildNodes.Add(node);
}
}
private void UpdateProperty(SPListItem user, Guid name, string value, List<string> updates)
{
if (value == string.Empty)
value = null;
string existing = user[name] == null ? null : user[name].ToString();
if (existing != value)
{
user[name] = value;
updates.Add(string.Format("{0}={1}", user.Fields[name].InternalName, value));
}
}
private void PopulateUser(object sender, TreeNodeEventArgs e)
{
SPListItem user = SPContext.Current.Site.GetCatalog(SPListTemplateType.UserInformation).Items[new Guid(e.Node.Value)];
foreach (Guid field in new Guid[] { SPBuiltInFieldId.ID, SPBuiltInFieldId.ContentType, SPBuiltInFieldId.IsActive, SPBuiltInFieldId.IsSiteAdmin, SPBuiltInFieldId.Title, SPBuiltInFieldId.Department, SPBuiltInFieldId.JobTitle, SPBuiltInFieldId.EMail, SPBuiltInFieldId.SipAddress })
{
TreeNode subNode = new TreeNode(string.Format("{0}: {1}", user.Fields[field].InternalName, user[field]));
subNode.Expanded = null;
subNode.PopulateOnDemand = false;
subNode.SelectAction = TreeNodeSelectAction.None;
e.Node.ChildNodes.Add(subNode);
}
}
</script>
</head>
<body>
<form id="MainForm" runat="server" method="post">
<SharePoint:formdigest id="SharePointDigest" runat="server" />
<div>
<asp:TreeView ID="UsersTreeView" runat="server" EnableClientScript="true" PopulateNodesFromClient="true"
OnTreeNodePopulate="PopulateUser" OnLoad="UpdateUsers" />
<asp:Button ID="UpdateBtn" runat="server" Text="Update" OnClick="UpdateUsers" />
</div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment