Thursday 6 March 2014

PowerShell Script To Upload Data From Local Drive To SharePoint Document Library

Below Script uploads all The Document from Your Local Drive Folder To SharePoint Document Library with error log file


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

#Script settings

$webUrl = "http://xyz:4444/"

$docLibraryName = "Documents"
$docLibraryUrlName = "Shared Documents\Test"

$localFolderPath = "C:\\Test\\Test"

#Open web and library

$web = Get-SPWeb $webUrl

$docLibrary = $web.Lists[$docLibraryName]

$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()

ForEach($file in $files)
{

    #Open file
    try
    {
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

    #Add file
    $folder =  $web.getfolder($docLibraryUrlName)

    write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
    write-host "Success"

    #Close file stream
    $fileStream.Close();
    }
    catch
    {
    Write "Error: $file.name: $_" >>c:\logfile.txt
        continue;
    }
}

#Dispose web

$web.Dispose()

 

PowerShell Script To download Bulk data from Document Library in SharePoint

Below Script Downloads All The Documents With all Sub folders of Document library. It also downloads CSV file which contains Metadata information of Downloaded documents. Also Catch block will Generate Error Log File .


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

$destination = "C:\\Test\\"
$web = Get-SPWeb -Identity "http://xyz:4444/"
$list = $web.GetList("http://xyz:4444/Shared Documents/")

function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
        $destinationfolder = $destination + "/" + $folder.Url
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory
        }
        #Download file
        try
        {
      
        $binary = $file.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: $_" >>c:\logfile.txt
        continue;
        }
     
        }
}
$exportlist = @()
$list.Items | foreach {
$obj = New-Object PSObject -Property @{
"Title" = $_["Title"]
"Name" = $_["Name"]
"Modified Date" = $_["Modified"]
"Modified By" =$_["Modified By"]
"Size"= $_["File Size"]
"Path" = $web.Url + "/" + $_.File.Url
}
$exportlist += $obj
$exportlist | Export-Csv -path 'C:\Test\MyList.csv' -noType
}

#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)
   
}

Wednesday 5 March 2014

SharePoint 2010 To SharePoint 2013 Step By Step Ugrade process


SharePoint 2010 to SharePoint 2013 Upgrade Step By step


Step1:

1.Backup Content Database of SharePoint 2010 from Sql Server Management 2008 R2

            1.Open Sql Server Management 2008 R2

            2.Right Click on your Content Database

            3.Task

            4.backup(Save as DatabaseName.Bak)

2.convert all your WSP solutions in 2013 from 2010

a.Open Visual Studio 2012

b.Edit yours .csproj file and add to it -> <TargetOfficeVersion>15.0</TargetOfficeVersion>

c.Reload the project again

d.In webpart.cs file,change the path of user control for eg :

private const string _ascxPath = @"~/_CONTROLTEMPLATES/15/SolutionName/Webpart/WebpartUserControl.ascx";

e.open WebpartUserControl.ascx source code and change all 14.0.0 references to 15.0.0
f.Double Click on Packages and change the Product Version to 15.

g. Right Click On your Project and Choose Properties. in Application Tab Choose Target FrameWork as 4.5
h. Click On build tab and Choose Platform Target As x64.
g. Publish it and save WSP (Example.WSP) on Your Storage Location.

 3.now Install That WSP in SharePoint 2013 Farm using Add-SPSolution "c:\Example.WSP" in Powershell ISE 64 Bit

4.Deploy all the custom WSP's on Your Web Application

Step 2:

1.Create New Web Application in SharePoint 2013(Don't create Site Collections)

Step 3:

Restore Your Database(DatabaseName.Bak)
a. Go To Sql Server Management 2012

b. Right Click On Database on top

c.Restore

d. Select your DatabaseName.Bak from Disk and start Restoring

Go to Powershell and Type

Test-SPContentDatabase -Name WSS_Content_DataBName -WebApplication http://WebApplName:90


(Change WSS_Content_DataBName with You DataBase Name and http://WebApplName:90 with Your WebApplication Name)

This will Test your database with webApplication.It will throw Warnings like Your New Web Application is claim Based etc

Note: By default 2013 is claim based and SharePoint 2010 is Classic Windows Authentication.

You can convert your New Web Application to Classic Windows Authentication using Powershell



Step 4:

Use Powershell ISE 64 bit or SharePoint 2013 Management Shell and type the following as it is

cd "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\BIN\"


Then Type the following

stsadm .exe-o addcontentdb -url http://WebApplName:90 -databasename WSS_Content_DataBName



OR


Run the below powershell command

Mount-SPContentDatabase "WSS_Content_DataBName" -DatabaseServer "SQLSERVER2012" -WebApplication http://testSite


If everything is fine, Operation will Complete Successfully Otherwise it will throw some errors.





Step 5:

Open your new Web Application In browser if Operation Completes Successfully.

Your Web Application Will Look Similar to the SharePoint 2010 .

Click On start Now button On Top in Red and follow The steps.

 Refresh it Several Times and Your web Application will get Ready if It doesn't throw Any error.

If there are any errors, rectify those and follow the start process again in web browser.



 Step 6: Migrate all users from classic to claims using below powershell



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

$WebAppName = "https://TestSite/"

$wa = get-SPWebApplication $WebAppName

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