Thursday, 10 January 2013

Activate MasterPage using Poweershell Script in SharePoint 2010


After deploying the custom Master Page we need to activate the Master Page for the particular site collection. After Activating the Custom Master Page, these are now visible in the Master Pages tab in Site Actions – Site Settings – Galleries – Master Pages. To set the custom Master Page as Default Master Page in SharePoint Foundation 2010, the user has to set it through SharePoint Designer. The User can also set the custom Master Page as Default using the following Powershell Script:

//For Single Site 

$web = Get-SPWeb http://sitename
$web.CustomMasterUrl = "/_catalogs/masterpage/custommasterpagename.master"
$web.MasterUrl = "/_catalogs/masterpage/custommasterpagename.master"
$web.Update()


//For All sites & Subsites within the site collection

$site = Get-SPSite -Identity http://sitename
foreach($subsite in $site.AllWebs)
{
    $subsite.AllowUnsafeUpdates = "True"
    $subsite.CustomMasterUrl = "/_catalogs/masterpage/custommasterpagename.master"
    $subsite.Update()
    $subsite.AllowUnsafeUpdates = "False"
    $subsite.Dispose()
}

Activate or Deactivate SharePoint Features Using Powershell


To deactivate the feature of the particular site use the following command using powershell:

Disable-SPFeature –Identity FeatureName –url http://sitename -Confirm:$False

To Activate the feature of the particular site use the following command using powershell:

Enable-SPFeature -Identity FeatureName -URL http://sitename -Confirm:$False

Powershell Script to Extract wsp from SharePoint Farm Solutions


Below is the Powershell Script used to extract the wsp solution from the SharePoint farm Gallery:

$farm = Get-SPFarm
$file = $farm.Solutions.Item("solutionname.wsp").SolutionFile
$file.SaveAs("E:\Backup\solutionname.wsp")

If the solution is not extracted from the solution using the above script which means the current user needs the admin rights to extract the solution. The Below command Adds a user to the  SharePoint_Shell_Access  role  for  the  specified  database:

Add-SPShellAdmin –UserName <username>

Exporting and Importing a Site, Library, or List using PowerShell


The SharePoint 2010 Central Administration UI is not the only way to export a site, library, or list. SharePoint 2010 provides PowerShell cmdlets to export or even to import them back whenever required. You can even script out these operations to run  manually or schedule them with the Windows Task Scheduler to make it run on a  scheduled basis.

Let's explore these cmdlets in detail.

The Export-SPWeb command is used to export a site, library, or list.

The Import-SPWeb command is used to import them back.

The general syntax of these commands are:

Export-SPWeb [-Identity]
<site-url> -Path<filename-&-location> [-Force] [–IncludeUserSecurity][-ItemUrl <List-or-Library-Name>]

Examples:
//Export site collection
Export-SPWeb -Identity http://site -Path E:\ Backup\sitename.cmp –Verbose
//Export Site Pages From particular site
Export-SPWeb -Identity http://site -Path E:\ Backup\sitename.cmp -ItemURL "Site Pages" –Force
//Export site with single backup file
Export-SPWeb http://site -Path E:\Backup\sitename.cmp -CompressionSize 1000 -Force –Verbose

Import Syntax:
Import-SPWeb [-Identity]
<site-url> -Path<filename-&-location> [-Force] [–IncludeUserSecurity][-Confirm]

Examples:
//Export site collection
Import-SPWeb -Identity http://site -Path E:\ Backup\sitename.cmp –Force  –Verbose
//Export Site Pages From particular site
Import-SPWeb -Identity http://site -Path E:\ Backup\sitename.cmp -ItemURL "Site Pages" –Force

Friday, 4 January 2013

Searcher.Filter users from Active Directory


Some different types of Searcher. Filter which filters the users from Active Directory according to the search query
//Search all the users from Active Directory
searcher.Filter = "(&(objectCategory=Person)(objectClass=User))";


//Search specific user from Active Directory
searcher.Filter = "(&(objectCategory=Person)(sAMAccountName=userloginname))";

//Get today’s Date
string modifieddatestring = SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.UtcNow);

modifieddatestring = modifieddatestring.ToString().Remove(10);
string sDate = modifieddatestring.ToString().Replace("-", "") + "000000.0Z";

//Search only the users which are created in Active Directory on the same day
searcher.Filter = "(&(objectCategory=Person)(objectClass=User)(whenCreated>=" + sDate + "))";

//Search only the users which are updated in Active Directory on the same day
searcher.Filter = "(&(objectCategory=Person)(objectClass=User)(whenChanged>=" + sDate + "))";

//Search only the users which are updated in Active Directory on the same day

string username = currentUser.LoginName.Split('\\')[1].ToString();//Get current user name
searcher.Filter = "samaccountname=" + username.ToString();


//Search & Load only specific properties of current user

searcher.PropertiesToLoad.Add("displayName");
searcher.PropertiesToLoad.Add("mail");
searcher.PropertiesToLoad.Add("employeeNumber");

SearchResult results = searcher.FindOne();
DirectoryEntry directoryEntry = results.GetDirectoryEntry();
//Display these properties in the lable control

lblEmpName.Text = directoryEntry.Properties["displayName"][0].ToString();
lblEmail.Text = directoryEntry.Properties["mail"][0].ToString();
lblEDP.Text = directoryEntry.Properties["employeeNumber"][0].ToString();

Create LDAP connection with Active Directory


Custom Code while creating a LDAP connection with Active Directory and get the Information of all the users.

DirectoryEntry de = new DirectoryEntry();

//DC = DomainController here AD Domain is sharepoint.2010.com,
de.Path = "LDAP://10.1.1.1/DC=sharepoint,DC=2010,DC=com";
de.AuthenticationType = AuthenticationTypes.Secure;
searcher = new DirectorySearcher(de);
//searcher.Filter  filters all the information according to the filter query. Below filter gets all users
searcher.Filter = "(&(objectCategory=Person)(objectClass=User))";
searcher.SearchScope = SearchScope.Subtree;
searcher.PageSize = 1000;