TechNet
Products
IT Resources
Downloads
Training
Support
Products
Windows
Windows Server
System Center
Microsoft Edge
Office
Office 365
Exchange Server
SQL Server
SharePoint Products
Skype for Business
See all products »
Resources
Channel 9 Video
Evaluation Center
Learning Resources
Microsoft Tech Companion App
Microsoft Technical Communities
Microsoft Virtual Academy
Script Center
Server and Tools Blogs
TechNet Blogs
TechNet Flash Newsletter
TechNet Gallery
TechNet Library
TechNet Magazine
TechNet Wiki
Windows Sysinternals
Virtual Labs
Solutions
Networking
Cloud and Datacenter
Security
Virtualization
Updates
Service Packs
Security Bulletins
Windows Update
Trials
Windows Server 2016
System Center 2016
Windows 10 Enterprise
SQL Server 2016
See all trials »
Related Sites
Microsoft Download Center
Microsoft Evaluation Center
Drivers
Windows Sysinternals
TechNet Gallery
Training
Expert-led, virtual classes
Training Catalog
Class Locator
Microsoft Virtual Academy
Free Windows Server 2012 courses
Free Windows 8 courses
SQL Server training
Microsoft Official Courses On-Demand
Certifications
Certification overview
Special offers
MCSE Cloud Platform and Infrastructure
MCSE: Mobility
MCSE: Data Management and Analytics
MCSE Productivity
Other resources
Microsoft Events
Exam Replay
Born To Learn blog
Find technical communities in your area
Azure training
Official Practice Tests
Support options
For business
For developers
For IT professionals
For technical support
Support offerings
More support
Microsoft Premier Online
TechNet Forums
MSDN Forums
Security Bulletins & Advisories
Not an IT pro?
Microsoft Customer Support
Microsoft Community Forums
Sign in
Home
Library
Wiki
Learn
Gallery
Downloads
Support
Forums
Blogs
Resources For IT Professionals
United States (English)
Россия (Pусский)
中国(简体中文)
Brasil (Português)
Skip to locale bar
Post an article
Translate this page
Powered by
Microsoft® Translator
Wikis - Page Details
First published by
Prasath C
When:
19 Sep 2013 10:20 PM
Last revision by
Naomi N
(eMicrosoft Partne)
When:
20 Sep 2013 8:03 AM
Revisions:
2
Comments:
0
Options
Subscribe to Article (RSS)
Share this
Can You Improve This Article?
Positively!
Click Sign In to add the tip, solution, correction or comment that will help other users.
Report inappropriate content using
these instructions
.
Wiki
>
TechNet Articles
>
Sync List with User Group Using jquery
Sync List with User Group Using jquery
Article
History
Sync List with User Group Using jquery
Requirement:
To get all the user names from the SharePoint group "Deliverable Owners" and sync with a custom list called "Team Members" in a button click.
Solution:
This is done using jquery and SharePoint web services.
<script type="text/javascript" src="https://siteurl/Documents/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="https://siteurl/Documents/jquery.SPServices-0.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#syncUsers").click(function() //'syncUsers' is the ID of button control added in the CEWP
{
var errorOccured = DeleteUsersFromTeamMembersList();
if(!errorOccured)
{
AddUsersToTeamMembersList();
}
});
});
// to delete the existing users from the custom list
function DeleteUsersFromTeamMembersList()
{
var errorOccured = false;
$().SPServices({
operation: "GetListItems",
async: false,
debug: true,
listName: "Team Members",
CAMLViewFields: "<ViewFields></ViewFields>",
completefunc: function (xData, Status) {
$(xData.responseXML).SPFilterNode("z:row").each(function() {
$().SPServices({
operation: "UpdateListItems",
async: false,
debug: true,
batchCmd: "Delete",
listName: "Team Members",
ID: $(this).attr("ows_ID"),
completefunc: function (xData, Status) {
msgData = xData.responseText;
isError = msgData.indexOf("ErrorText");
if (isError > 0) {
// Error creating fields!
alert("Error deleted user names from Team Member list!");
errorOccured = true;
}
}
});
});
}
});
return errorOccured;
}
//add all the users from the sharepoint group to custom list.
function AddUsersToTeamMembersList()
{
var alreadyProcessed = false;
var userName ;
var iD;
var userID;
var errorOccured = false;
$().SPServices({
operation: "GetUserCollectionFromGroup",
groupName: "Deliverable Owners",
completefunc: function (xData, Status) {
$("#results").text(xData.responseXML.xml);
$(xData.responseXML).find("User").each(function () {
userName = $(this).attr("LoginName");
iD = $(this).attr("ID");
userID = iD +";#"+userName;
$().SPServices({
operation: "UpdateListItems",
async: false,
batchCmd: "New",
listName: "Team Members",
valuepairs: [["User",userID ]],
completefunc: function(xData, Status)
{
msgData = xData.responseText;
isError = msgData.indexOf("ErrorText");
if (isError > 0) {
// Error creating fields!
alert("Error synchronizing user names!");
errorOccured = true;
}
}
});
});
if( errorOccured == false)
{
alert("User name synchronized successfully.");
$(window.location).attr('href', 'currentpagename.aspx');
}
}
});
}
</script>
Limitations & Prerequisite
- The performance of this code will get affected based on the size of the user group.
- The Team Members custom list must have "User" field with people and group type.
en-US
,
has code
,
JQuery
,
SharePoint 2007
,
SharePoint 2010
[Edit tags]
Leave a Comment
Please add 8 and 3 and type the answer here:
Post
Wiki - Revision Comment List(Revision Comment)
Wikis - Comment List