Friday 13 March 2015

Powershell for downloading files step by step from sharepoint document library

Below Powershell will download the file Test.pdf from List/Document library name "DocName" in Root folders named A,B and C (You can change the letters in powershell script).


if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}

$destination = "L:\Data\"
$web = Get-SPWeb -Identity "https://TestWebApp/"
$list = $web.GetList("https://TestWebApp/Test/DocName") #your List/Document Library path

function ProcessFolder
{
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files)
   
    {
   
         if($file.Name.Contains("Test.pdf"))
            {                                    
                 [Microsoft.SharePoint.SPFileVersion]$iv = $file.Versions[0];
                 $versionNumber = $iv.VersionLabel;    
                                   
             
               Write-Host    $versionNumber.Name
               if($versionNumber )
               {
             
               DownloadFile($iv)
               }
               else
               {
           
               DownloadFile($file)
               }
             }

      else
               {
         
               DownloadFile($file)
               }
                               

       
       }

    }
 

     Function DownloadFile($type)
     {
     try
     {
      $destinationfolder = $destination + "/" + $folder.Url
      if (!(Test-Path -path $destinationfolder))
        {
           $dest = New-Item $destinationfolder -type directory
         }

     $binary = $type.OpenBinary()
     $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
     $writer = New-Object System.IO.BinaryWriter($stream)
     $writer.write($binary)
     $writer.Close()

     }
     Catch
     {
      Write "Error: $file.name: $_" >>"L:\Data\Error.txt"
      continue;
     }

     }
       
        #Ensure destination directory


function ProcessAllSubfolders($folderCollection)
{
    foreach ($folder in $folderCollection)
    {
        if ($folder.SubFolders.Count -gt 0)
        {
            ProcessAllSubfolders($folder.SubFolders)
        }

        ProcessFolder($folder.Url)
    }
}


#Download root files
#ProcessFolder($list.RootFolder.Url)
#Download files in folders

foreach($folder in $list.RootFolder.SubFolders)
{
if($folder.Name.StartsWith("A") -or $folder.Name.StartsWith("B") -or $folder.Name.StartsWith("C"))
{
ProcessFolder($folder.Url)

if ($folder.Subfolders.Count -gt 0)
        {
            ProcessAllSubfolders($folder.SubFolders)
        }

}

}

Write-Host "Completed Succesfully"


 
 

Powershell for migrating users in claims in sharepoint

Following powershell will migrate classic mode users into Claim based in sharepoint

if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$WebAppName = "https://testspsite/"

$wa = get-SPWebApplication $WebAppName

#Final step is to trigger the user migration process
$wa = get-SPWebApplication $WebAppName
$wa.MigrateUsers($true)

Thursday 12 March 2015

List Threshold Limits & Workarounds in Sharepoint

Minimum Limit is 2000 and Maximum limit is 2147483647
list view threshold by default is 5000 and 20000 for auditors and administrators.
Effect on performance :The limitation actually comes from sql server. if SQL Server executes a query on a single table that would exceed 5,000 results, SQL Server will lock the entire table while the query executes. Since SharePoint stores all list data in a single table, a single query that returns over 5,000 items will lock all of the list and library data for that entire content database! Users who are accessing content in SharePoint while the SQL Table is locked will have to wait longer for their results to be returned.
Avoiding List Threshold Issues:
By carefully configuring your list views, you can return the data that users need to see while keeping under your threshold limits for that list or library. Here are some tips to consider while planning the structure for your content.
Separate your content into multiple lists and libraries.

Create multiple list views. Each view can show the content in
different ways allowing users to choose what they prefer to see.
Some common example may include Recently Changed, Recently Added,
Created By Me, Assigned to Me, Due Today.

Add filters to your list views to limit the content that is
returned.

Use AND when defining your filters. Choosing OR will return all
results first before your filter is applied.

The first criteria in your view filters should have the greatest
impact on reducing the number of items returned.

Add an Index to any columns used for sorting or filtering your
views. SharePoint automatically works some magic in SQL Server when
you create an indexed column which allows it to quickly analyze and
return values, bypassing thresholds.

Wednesday 11 March 2015

Powershell for migrating Users in sharepoint 2010 to another domain

Below powershell script is used to migrate users from one domain to another in sharepoint 2010.



 if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}



$users = Get-SPUser -web http://cp2010:6070/

foreach ($User in $Users) {
$domainName=$user.loginname.split('\')[0]
$userName=$user.loginname.split('\')[1]

if ($domainname -eq 'Test1')
{
#write-host $userName
$name='Test2\'+$userName
Move-SPUser -IgnoreSID -Identity $user -NewAlias $name
}
}


Just replace the Test1 to your old domain name and Test2 with your new domain name

Migrate Active Directory group in Sharepoint 2013 Using PowerShell

Below Powershell Script migrates AD group in one domain to another in Sharepoint 2013



if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

 $farm = Get-SPFarm

$farm.MigrateGroup('TestDomain1\Group', 'TestDomain2\Group')


Branding Mysite with feature stapling in Sharepoint 2013


Here is a quick guide for branding SharePoint 2013 My Sites using Feature Stapling.






Create a new Sharepoint Solution.


Select "Farm solution" then provide the correct URL to your MySite Host (in my case I'm using "/my") then click Finish.

Step 1 : Provisioning the custom masterpage


Add a new Module to the projet.


As a starting point just copy the original my sites masterpage from "\15\TEMPLATE\FEATURES\MySiteMaster\mysite15.master" into the project and rename it "CustomMySite.master" and integrate your HTML/CSS design to it. EDIT : if you are adding a new file (css, png, jpg,...) that you are referencing in the master consider using the LAYOUTS folder to keep url reference simple. Alternatively you can use a provisioning feature to store it in a library instance on the site but this method can be unsafe as the url may be inconsistent and therefore will require to unghost the masterpage to edit the html.

Edit the Elements.xml and copy this:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="ML_MasterPages" List="116" Url="_catalogs/masterpage" Path="ML_MasterPages" RootWebOnly="TRUE">
    <File Url="CustomMySite.master" Type="GhostableInLibrary" />
  </Module>
</Elements>



You'll have to add the module to the feature you will create in step 2.

Step 2 : Applying the custom masterpage to the site
We'll use an event receiver to apply the new master.


Add a new Feature with scope "Site" and name it "Site_ApplyCustomMasterPage";


Add an event receiver to the feature.

Add the following code:

namespace ClientName.MySitesBranding.Features.Site_ApplyCustomMasterPage
{
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using System;
    using System.Runtime.InteropServices;
    using System.Security.Permissions;

    [Guid("{your Guid}")]
    public class Site_ApplyCustomMasterPageEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = (SPSite)properties.Feature.Parent;
            string masterUrl = SPUrlUtility.CombineUrl(siteCollection.ServerRelativeUrl, "_catalogs/masterpage/CustomMySite.master");

            foreach (SPWeb web in siteCollection.AllWebs)
            {
                try
                {
                    web.MasterUrl = masterUrl;
                    web.CustomMasterUrl = masterUrl;
                    web.Update();
                }
                catch
                {
                    if (web != null)
                        web.Dispose();

                    throw;
                }

                if (web != null)
                    web.Dispose();
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite siteCollection = (SPSite)properties.Feature.Parent;
            string masterUrl = SPUrlUtility.CombineUrl(siteCollection.ServerRelativeUrl, "_catalogs/masterpage/mysite15.master");

            foreach (SPWeb web in siteCollection.AllWebs)
            {
                try
                {
                    web.MasterUrl = masterUrl;
                    web.CustomMasterUrl = masterUrl;
                    web.Update();
                }
                catch
                {
                    if (web != null)
                        web.Dispose();

                    throw;
                }

                if (web != null)
                    web.Dispose();
            }
        }
    }
}

Step 3 : Stapling the "apply mastepage" feature to the personal site template
This action will hook your feature to any new personal site that will be created after staple is effective. If you need to apply the branding to existing personal write a PowerShell script that activates the feature for each "/my/personal/xxx" :)
NOTE : As the MySiteHost (the "/my" root url) will usually already exist on the customer's target platform when they create their "User Profile Application Service" you'll have to manually activate the "ApplyCustomMasterPage" feature on that site collection (or think PowerShell if needed)



Create a new Feature with scope "Farm" and name it "Farm_MySiteStapler".



Then create a new empty Element name it "ML_ApplyCustomMasterPageStapler".

Add the followin code

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <FeatureSiteTemplateAssociation Id="{ Site_ApplyCustomMasterPage feature ID}" TemplateName="SPSPERS#2" />
</Elements>
Note that in SharePoint 2013 the Template to staple is "SPSPERS#2" whereas in SharePoint 2010 you had to staple "SPSPERS#0".

You will get Feature id in menifest.xml




Add the module to the Stapler feature.

Your projet should look like this:

 At this point the branding will actually work for the root sites, if you wish to apply template to any subsite that will be created (such as Blogs,...) you need to follow step 4 to hook the WebProvisioned event.


Step 4 : Applying the masterpage to any subsite created under the my site


Add a new Event receiver "ER_MySite_ElementsWebProvisioned".


Selected the web event "a site was provisioned".

Then enter the following code:

namespace ClientName.MySitesBranding.Receivers.ER_MySite_ElementsWebProvisioned
{
    using System;
    using Microsoft.SharePoint;

    public class ER_MySite_ElementsWebProvisioned : SPWebEventReceiver
    {
        public override void WebProvisioned(SPWebEventProperties properties)
        {
            SPWeb web = properties.Web;
            SPWeb rootWeb = web.Site.RootWeb;
            web.MasterUrl = rootWeb.MasterUrl;
            web.CustomMasterUrl = rootWeb.CustomMasterUrl;
            web.Update();
        }
    }
}


Now add the Event receiver to the "Site_ApplyCustomMasterPage" feature.

Your project should look like this: 


Deploy your solution, activate the Farm feature and connect with a new user to his MySite check that the new master is applied. 


For already created site, you need to run powershell for activating the feature. :)


Reference :http://sharepointologic.blogspot.in/2013/04/branding-sharepoint-2013-my-sites-with.html

Adding Webpart to webpart pages through powershell

The following powershell will add Custom visual webpart to a webpart page.


if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$Url = "http://sp2013:5559" #Please remove trailing slash if any
$web = Get-SPWeb $Url
$web.AllowUnsafeUpdates=$true

function Add-WebPartToPage($pageUrl, $webpartzone,$index,$fileName)
{
    $webPartGallery = $web.Lists["Web Part Gallery"]
   
    Write-Host "Searching webpart $fileName in web part gallery" -ForegroundColor Yellow
    if($webPartGallery -eq $null)
    {
        Write-Host("Unable to retrieve Webpartgallery");
    }
    $webpart = $null;
    $webpart=$webPartGallery.Items | ? { $_.Title -eq $fileName}

    if($webpart -eq $null) {
        Write-Host("Unable to retrieve webpart: $fileName") -ForegroundColor Red
    }
    else 
{
Write-Host("----------Adding Webpart--------")-ForegroundColor Yellow
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$errorMsg = "";
$xmlReader = New-Object System.Xml.XmlTextReader($webpart.File.OpenBinaryStream());
 $webpart = $webpartmanager.ImportWebPart($xmlReader,[ref]"Error")
$webpartmanager.AddWebPart($webpart, $webpartzone, $index);
Write-Host($fileName +" Webpart is added successfully") -ForegroundColor Green ;

   $wp=$webpartmanager.WebParts[1];
    $wp.ChromeType="None";

    $webpartmanager.SaveChanges($wp);

       }
}

$SiteURL =$Url
#---------------Test Page----------------------------
$PageName="TestPage.aspx"
#$page=$web.lists["SitePages"].Items | ? {$_.Name -eq $PageName} 

Add-WebPartToPage "$SiteURL/SitePages/$PageName" "Header" 0 "Test Form"

$PageName="TestPage.aspx"