Monday, March 27, 2017

GetSPFieldDetailsForAllLists

function GetSPFieldDetailsForAllLists($SiteCollectionURL)
{
    $site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL) #Change site URL#
    $web = $site.openweb()
   
    foreach ($list in $web.Lists) #Get all list in web
    {
        foreach ($view in $list.Views) #Get all views in lists
        {
            $spView = $web.GetViewFromUrl($view.Url) #Grab views URL
            Write-Host "List Name: " $list.Title  ##Print List title
            Write-Host "------------------------------------------------------"
            Write-Host "Field Name | Field Title " -ForegroundColor DarkGreen
            Write-Host "------------------------------------------------------"
            foreach ($spField in $spView.ViewFields) #Loop through all view URLs and get Fields (columns)
            {
                foreach ($field in $list.Fields) #Get all fields in lists
                {
                    if($spField -eq $field.Title) #if field in lists equals field in views
                        {
                            Write-Host $spField " | " $field.Type -ForegroundColor Green #Write out each field (column)                        
                        }
                }
            }
            Write-Host "------------------------------------------------------"
            Write-Host " "
        }
    }
    $web.Dispose()
    $site.Dispose()
}

For loops Powershell

For loops can be used (just like in any other programming language) to have a set process run for a finite number of times.
The structure of a for loop looks like this:
for (initial value; run until condition; repeat)
To run a for loop in Powershell, use the following example which prints out the first 5 numbers:
for ($i = 1; $i -lt 6; $i++)
 {
 Write-Host $i
 }
Output:
1
2
3
4
5

Show Page View Count based on location

Create List Structure:

List Title : "PageAnalytics"

Columns Type

Title(Country) (Single Line of Text)

PageTitle (Single Line of Text)

UserID (Number)

Script to Save Current User Location on Page View and Also Fetch Page View Details

});//Fetching Current Users Location from site 'http://freegeoip.net'
$.getJSON("http://freegeoip.net/json/", function(data) {
var countryName = data.country_name;
//In case any other details need to be saved or displayed other than country name
/*
var country_code = data.country_code;
var ip = data.ip;
var time_zone = data.time_zone;
var latitude = data.latitude;
var longitude = data.longitude;
*/
storeCurrentUser(countryName);
getPageViews();
});
//Adding Page View Detail of current user to List
function StoreCurrentUser(countryName){
var currentPage=$(document).attr('title');
var currentUser=_spPageContextInfo.userId;
var data = {
    __metadata: { 'type': 'SP.Data.PageAnalyticsListItem' },
    Title: countryName,
    PageTitle: currentPage,
    UserID: currentUser
};
$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('PageAnalytics')/Items",
    type: "POST",
    headers: {
        "accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "content-Type": "application/json;odata=verbose"
    },
    data: JSON.stringify(data),
    success: function (data) {
        console.log(data);
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
}
//Fetching current Page view from list filtered by Page Title
function getPageViews(){
var currentPage=$(document).attr('title');
var countryArray=[];
var userArray=[];
var viewCount={};
$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('PageAnalytics')/Items?$orderby=Title&$filter=PageTitle eq " + currentPage,
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: function (data) {        
        console.log(data.d.results);
        $.each(data.d.results,function(i,val){
            //for unique Page Unique User Count                
            if(userArray.indexOf(val["UserID"]) < -1 && countryArray.indexOf(val["Title"]) < -1){
                userArray.push(val["UserID"]);
                countryArray.push(val["Title"]);
                viewCount[val["Title"]]={
                    Country : val["Title"],
                    Count : 1;
                }
            }
            else{
                viewCount[val["Title"]]["Count"]=viewCount[val["Title"]]["Count"]+1;
            }
            //Unique page and Non Unique Users
            /*if(userArray.indexOf(val["UserID"]) < -1 && countryArray.indexOf(val["Title"]) < -1){
                userArray.push(val["UserID"]);
                countryArray.push(val["Title"]);
                viewCount[val["Title"]]={
                    Country : val["Title"],
                    Count : 1;
                }
            }
            else{
                viewCount[val["Title"]]["Count"]=viewCount[val["Title"]]["Count"]+1;
            } */
        });
        //final Page wise count can be found here
        console.log(viewCount);
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
});
}

upgrade workflow manager


  1. install Service Bus 1.0 CU 1 on all workflow servers using web platform installer. Reboot
  2. install Workflow Manager 1.0 Refresh on all workflow servers using web platform installer Reboot
  3. install Workflow Manager Client 1.0 Refresh on all workflow servers using web platform installer (Do we need to install this tool in SharePoint Servers too)
  4. Run (Register-SPWorkflowService) in SharePoint Servers.

Thursday, March 16, 2017

sharepoint Powershell migrate users in a web application

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({
param([string]$url = "http://sharepointsite/", `
[string]$oldprovider = `
"keystoneuser", `
[string]$newprovider = `
"i:0ǵ.t|keystone sts|radiant\")

add-pssnapin microsoft.sharepoint.powershell -EA 0

# Get all of the users in a site
#$users = get-spuser -web $url -Limit "11000"
$csvFile = "C:\Users\Users.csv"
# Loop through each of the users in the site
Import-Csv $csvFile | ForEach-Object{
# Create an array that will be used to split the user name from the domain/membership provider
$a=@()
$username = $_.oldlogin
$a = $username.split(":")
$userloginprovider = $a[0]
$displayname = $a[1]
Write-Host "Old Username: $username."
# Create the new username based on the given input
$newalias = $newprovider + $displayname
$user = Get-SPUser -Identity $username -Web http://
sharepointsite/ -EA "SilentlyContinue"
if($user -notlike "")
{
Move-SPUser –Identity $user -NewAlias "$newalias" -IgnoreSID -Confirm:$false -EA "SilentlyContinue"
Write-Host "User Changed to $newalias"
}
}
}); 




To migrate user in farm use the following,


$farm.MigrateUserAccount( $_.oldlogin, $_.newlogin, $false )