Search This Blog

Taking Backup of All Sharepoint Site Collections Simultaneously

Some days before I face a issue to format my PC. I felt to tedious to take backup of all my webapplications and site collections from my SharePoint Server, as I have many web application and so many site collections on those web application.

I also don't want to lose any single site collection, as they can be required or useful any time.

For Solving this Issue I do some R&D and create a PowerShell Script which Automatically iterate from all web applications and site collections and take their backups, I am sharing it for all my blog visitors.

<# Script to take backup of all web application and sites from the sharepoint server. #>

$FolderPath = "E:\Backup\" // Folder path to take Backup of all Objects
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
                Write-Host "Loading SharePoint Powershell Snapin..."   
                Add-PSSnapin "Microsoft.SharePoint.Powershell"
foreach ($SPWebApplication in (Get-SPWebApplication)) {
    foreach ($Site in $SPWebApplication.Sites) {
        $Filename = $FolderPath + $SPWebApplication.Name.Replace(" ","") + " " + $Site.ServerRelativeUrl.Replace("/","_")+ ".bak"
        Write-Host "$Filename"
        backup-spsite -identity $Site.URL -path $FileName -Force

I am also planning to add script to take backup of all the wsp's from the central admin if anyone required, so that can be redeploy in new server easily. 

Please give your comments regarding these scripts.

Write different code for Debug and Release Mode

Many time a developer needs to write a code in which he wants something different in Debug Mode and Something different in Release Mode

Most probably this is used at the time of initializing objects or displaying errors.

1. In debug mode I want to Initialize an array with some random numbers and In release mode I want to get array values from user.  It is not possible to change code every time you give in release or test in debug.
2. Same in Exception handling We want to show detailed error message in debug  mode but minimum error message after release. (For Security Reasons.).

In C# there #if DEBUG is a solution for the above problem.


Common Programming Tips for C#.

Here is some most common programming errors and tips for c# developers.

Use Convert.ToString() instead of .ToString()
Convert.ToString function handles null values whereas .ToString() cannot handle null values and throw error "Object Reference not set to an instance of an object."

Use StringSplitOptions.RemoveEmptyEntries while splitting the string
When we split some string and perform some operations on each portion of string, using StringSplitOptions.RemoveEmptyEntries is a good practice this will remove empty strings from the returned Array, and remaining operations does not give an error.

You can see difference in results. In first case you need to check whether portion is not Empty, if it is it may throw an error. Also it returns count with empty strings so logic for program must change accordingly.

Use TryParse instead of Convert function for converting Int, double, boolean etc.
If the object is null or not valid value for conversion this functions throw an error while TryParse returns 0 in case of null or wrong input.

FBA (Form Based Authentication) Login through Web Service in Sharepoint

Here is one nice method for login in Sharepoint through Web Service. But the case is FBA (Form Based Authentication).

When we try to login in SharePoint Using WebService, It's very easy in case of Active Directory Based User Login because AD Login need one line of code to pass credentials.

But What about Form Based Authentication ?

When the case is FBA (Form Based Authentication) cookies comes in Scenario...
It is totally different then AD Login.

Here is one small function I used for FBA login in Sharepoint using Web Service....

Step 1 : First you need to add one Web Service Referance for Authentication.asmx

Step 2 : Use this function

Add Taxonomy Field / TaxonomyWebTaggingControl to the Web part or Application Page

Here is a short code snippet to add A TaxonomyWebTaggingControl (Taxonomy Field) to the webpart/ custom control or application page.

Step 1 : Add Reference of the Assemble : Microsoft.SharePoint.Taxonomy

Step 2 : Register Taxonomy to the page.

Step 3 : Add TaxonomyWebTaggingControl to the page

Other detailed Properties can be found at taxonomywebtaggingcontrol properties.

Step 4 : Now Assigning Managed Mata Data to TaxonomyWebTaggingControl.

This can be done in two ways.
1. Statically
2. Using Code ( Dynamically).

Set four properties in the above code.
* Must Set
SspId : TermStore Id (in form of Guid)
TermSetId : TermStore Id (in form of Guid)

* Set only if IsDisplayPickerButton value is true.
SSPList : TermSet Id (in form of Guid)
TermSetList : TermSet Id (in form of Guid)

Using Code ( Dynamically).

use this code to set these values at runtime (in Page Load Event)

Note :

* Managed Metadata Service // TermStore Name
* tGroup //TermStore Group
* tTermSet //TermSet within Group

Enjoy, you can now run the application and check the output.

Get User from user field

Getting user from User field of SharePoint list or Library.

This function will return SPUser Object or Null if any error occurs
Link Within Related Posts Plugin for WordPress, Blogger...