Asked by:
Add users to group

Question
-
Hi all,
I have used below code to add users to group in sharepoint successfully.
<script type="text/javascript">
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems);
function retrieveListItems(){
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('MyTestList');
var camlQuery = new SP.CamlQuery();
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args){
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var siteCollUrl=oListItem.get_item('siteCollUrl');
addUserToSharePointGroup(siteCollUrl);
}
}
function addUserToSharePointGroup(siteUrl){
var clientContext = new SP.ClientContext(siteUrl);
var collGroup = clientContext.get_web().get_siteGroups();
var oGroup = collGroup.getById(7);
var userCreationInfo = new SP.UserCreationInformation();
userCreationInfo.set_email('alias@somewhere.com');
userCreationInfo.set_loginName('DOMAIN\alias');
userCreationInfo.set_title('John');
this.oUser = oGroup.get_users().add(userCreationInfo);
clientContext.load(oUser);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onAddSucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onAddSucceeded() {
console.log(this.oUser.get_title() + " added.");
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
But in my environment my login name is like i:0#.w|test|kkumar.with out passing domain name how to add users.Please help.
regards,
Praveen
All replies
-
Hi,
you cannot hard code the domain name in code and pass only username?
- Edited by Ullas Kottary Thursday, December 20, 2018 10:59 AM
-
-
You can append the domain in your code right? like below
function doSomething(userName)
{
var validName="domainName\"+userName;
}
- Edited by Ullas Kottary Thursday, December 20, 2018 11:18 AM
- Proposed as answer by Wendy DZMicrosoft contingent staff, Moderator Thursday, December 27, 2018 9:36 AM
-
iam getting security validation error with full control permissions as well as iam following instructions in the below url
http://sharepoint-works.blogspot.com/2012/04/security-validation-for-this-page-is.html.
but its not a good practice.please suggest any other solution.thaks in advance.
-
-
Hi Praveenrangu,
SPSecurity.RunWithElevatedPrivileges is used in Server Object Model, as your code is JavaScript Client Object Model, this is not the right way to fix this error.
And let's back to the original question, as Ulas's suggestion, you could use "Domain\"+username for the login name.
If this is helpful to the question, I would suggest to Mark as answer so that it could help others who stuck with the same question in the forum.
About the new error occurred, I would suggest you could create a new thread which will focus on the new question.
Thanks
Best Regards
Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.
Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.- Proposed as answer by Wendy DZMicrosoft contingent staff, Moderator Thursday, December 27, 2018 9:36 AM
-
Hi,
I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.
You can mark the helpful post as answer to help other community members find the helpful information quickly.
Thanks
Best Regards
Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.
Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.