Sunday, December 23, 2018

Update SharePoint User Profile work email into specific Web Application

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  
$WebAppURL="http://contoso.com"
$UserAccount="i:0#.w|Contoso\Nagaraju.Pattem"
 
#Update Email for all sites in the web application
Get-SPWebApplication $WebAppURL | Get-SPSite -Limit All | Foreach-object {
     
    Write-host "Processing:" $_.Rootweb.URL
 
    #Get the User's Current Display Name and E-mail
    $User = Get-SPUser -Identity $UserAccount -Web $_.Rootweb.URL
 
    if($User -ne $null)
    {
        Set-SPUser -Identity "i:0#.w|Contoso\NAgaraju.Pattem" -Email "Naga@Contoso.com" –Web $_.Rootweb.URL
    }
}

Tuesday, December 11, 2018

How to check if user exists in SharePoint groups or not using JSOM


<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(LoadGroups, "sp.js");

var group;
var users;
var ctx;
var groupCollection;
var user;
var currentuser;
//Add SharePoint Group id into below array
var userGroupIDs = [5,3];
var redirectPage = true;
var currentIndex = 0;
function LoadGroups() {

    IsUserExists(userGroupIDs[0]);
}

function IsUserExists(groupID) {
    ctx = SP.ClientContext.get_current();
    groupCollection = ctx.get_web().get_siteGroups();
    currentuser = ctx.get_web().get_currentUser();

    group = groupCollection.getById(groupID);
    ctx.load(group);
    ctx.load(currentuser);
    ctx.executeQueryAsync(Function.createDelegate(this, this.OnGetGroupSuccess), Function.createDelegate(this, OnFailure));
}

function OnGetGroupSuccess() {
    users = group.get_users();
    ctx.load(users);
    ctx.executeQueryAsync(Function.createDelegate(this, this.OnGetuserSuccess), Function.createDelegate(this, OnFailure));
}

function OnGetuserSuccess() {
debugger
    currentIndex++;
    var userEnumerator = users.getEnumerator();
    while (userEnumerator.moveNext()) {
        var user = userEnumerator.get_current();
        if (user.get_id() == currentuser.get_id()) {
            redirectPage = false;
            //alert("User Exists");
            break;
        }
        else {
        }
    }

    if (currentIndex == userGroupIDs.length) {
        if (redirectPage) {
            redirectLandingPage();
        }
    }else{
        IsUserExists(userGroupIDs[currentIndex]);
}

}

function OnFailure(sender, args) {
    //alert("Failed to execute IsCurrentUserMemberOfGroup method");

    currentIndex++;
    if (currentIndex == userGroupIDs.length) {
        if (redirectPage) {
            redirectLandingPage();
        }
    }else{
        IsUserExists(userGroupIDs[currentIndex]);
}
}

function redirectLandingPage() {
    window.location.href = "https://contoso";
}
</script>