Saturday, August 24, 2013

PowerShell Script to backup WSP from Central Admin's solution gallery


For a specific solution backup

$solution = Get-SPSolution $packageName -ErrorAction SilentlyContinue
# Get the File name from Solution file.
$filename = $solution.SolutionFile.Name

# script to get the location where the script is running -- folder location.
$BaseFolder = (Get-Location -PSProvider FileSystem).ProviderPath

# Save the file to Disk
$solution.SolutionFile.SaveAs("$BackupFolder\$filename")


To backup all the solutions from central admin' solution gallery

$BaseFolder = (Get-Location -PSProvider FileSystem).ProviderPath
$SPSolutions = get-spsolution
foreach($solution in $spsolutions)
{
$filename = $solution.SolutionFile.Name
$solution.SolutionFile.SaveAs("$BackupFolder\$filename")
}

Tuesday, August 13, 2013

Power Shell Script to Hide NewsFeed, Sites, Sky Drive top menu bar in SharePoint 2013

In order to hide the menu bar , We have to make below node text to empty by using PowerShell

i,e. Div tag shoud look like
TODO:
$WebApp =  get-spwebapplication("Any Specific WebApp URL")
$WebApp.SuiteBarBrandingElementHtml = ""
$WebApp.Update()

This can be achieved using Java Script on Master Page or by editing the same in SharePoint designer.

Monday, July 9, 2012

Start a specific service on all SharePoint Servers in a FARM (WFE and APP Servers) using PowerShell


//Services Like SPTimerV4, SPAdminV4, iisadmin, W3SVC, WAS
        $serviceName = "Name of the service to be started"
        // Loop thru all the servers in a SharePoint Farm
        foreach ($server in (get-spserver | Where {$_.Role -ne "Invalid"}) )
        {
            Write-Host Restarting $serviceName in $server.Name
            $service = Get-WmiObject -computer $server.Name Win32_Service -Filter "Name='$serviceName'"
            $service.InvokeMethod('StopService',$Null)
            Start-Sleep -s 5
            $service.InvokeMethod('StartService',$Null)
            Start-Sleep -s 5
            $service.State           
        }



Upgrading SharePoint Feature using PowerShell


$site = get-spsite "http://mysite"
$features = $site.QueryFeatures("web", $true);

foreach($feature in $features)
{
        if($feature.Definition.DisplayName -eq "FeatureName")
        {       
            $feature.Upgrade($true);
        }
}

Thursday, February 3, 2011

Could Not Connect to Sharepoint from Infopath 2010

Some times when we trying to connect to sharepoint site from infopath we will get an error saying Operation could not be completed as shown below


This means that top level site is not created.
To test this
Browse the url http://servername in my instance it could be http://win2008r2:11442 if we get page cannot be displayed message, top level site is not created without which we can't connect to sharepoint site from a infopath form.
To resolve this go to central admin and
  • click on create a site collections in application management tab
  • Provide Title and Description
  • select URL field without sites option from the URL dropdown.(in my case it could be http://win2008r2:11442/ )
  • Select a Template
  • Give primary and secondary Site collection administrators and click on OK.
dats it, u have created top level site.
Now move on to ur infopath form and try to connect to sharepoint.



Thursday, October 29, 2009

Creating a SharePoint page using Object Model

SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb web = SPContext.Current.Web.Site.RootWeb;
//SPContext.Current.Web;
using (web)
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["Pages"]
String url = list.RootFolder.ServerRelativeUrl.ToString();
// if you want to add the new page in a existing folder you can take it by it's ID like below
//SPListItem newFolder = list.Folders.GetItemById(3);
// creating a publishing web
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web)
PageLayout[] layouts = publishingWeb.GetAvailablePageLayouts();
PageLayout layout = null;
foreach (PageLayout _layout in layouts)
{
//set the page layout to your site's default layout
if
(_layout.Name == "yourpagelayout.aspx")
{
layout = _layout;
break;
}
}
PublishingPage newPage = publishingWeb.GetPublishingPages().Add("sample.aspx", layout);
// setting the URL value to the newPageUrl for redirecting the user after creating it
//newPageUrl = web.Url + "/" + newPage.Url;
newPage.Description = "Sample page from code behind Dynamic Page";
newPage.Title = "Dynamic Page";
newPage.Update();
//Now we can checkin the newly created page to the “pages” library
SPFile pageFile = newPage.ListItem.File;
if (pageFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
pageFile.CheckIn("CheckedIn");
pageFile.Publish("publihsed");
}
}
});

Querying a SharePoint list using CAML (Collaborative Application Markup Language) builder and updating a List using SharePoint Object model.

To Do: First we must have a U2U CAML Query Builder or one must be familiar with CAML query language syntax. Download CAML builder http://www.u2u.info/SharePoint/U2U%20Community%20Tools/U2U%20Caml%20Query%20Builder%202007%20v3.1.0.0%20(windows%20version).zip
Extract the zip file from above download and
click on U2UCamlCreator.exe which opens the below screen


Now enter the URL of your SharePoint site Ex: http:/servername//sites/mysite/ and use the default settings and click on connect. This will open up all the lists associated with the site as shown below

Now select the desired list on which you want to perform a query. In this case I am selecting a list existing on my site leavenotification and then CAML builder shows all the columns of the list as shown below.



Now select the columns which you require for your application and you can set the where condition to build the query with an option of order by. In my case I am selecting Date From column with a where condition as date lower than or equal as shown below

To DO: 1. Select the column to be filtered

2. Select the Where check box

3. Select a condition for search criteria and click on > button Above 3 steps will generate a XML based CAML query.


To test this query click on Test button and the result will be displayed on a grid as shown below At any point of the time you can change the query by clicking Clear button on top right corner


Using SharePoint object model to query a list with the query generated from CAML builder.
Import Microsoft.sharepoint.dll
Code Part:
Try
{

SPWeb objSPWeb = SPContext.Current.Web.Site.RootWeb;
objSPWeb.AllowUnsafeUpdates = true;
SPList objSPList = objSPWeb.Lists["LeaveNotification"];
SPQuery queryErrData = new SPQuery();
string QueryOrderBy = " 1 "
SPListItemCollection objlistItemsErrLog = objSPList.GetItems(queryErrData);



}
catch
{
throw ;
}
From the above ListItemCollection we can access the result set by looping thru the collection using foreach