Category Archives: PowerShell

Creating a Search Content Source of type Web using PowerShell

The New-SPEnterpriseSearchCrawlContentSource command in Powershell is used to create a new crawl content source for a SharePoint search application.We can view the whole set of parameters and options in this msdn article.

So , when we need to create a SharePoint Search content source, using Powershell, we use the following commands-

[sourcecode language="xml"]
$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
New-SPEnterpriseSearchCrawlContentSource -SearchApplication $searchapp -Type SharePoint -name ContentSourceWithTypeSharePoint -StartAddresses http://localhost:6001/
[/sourcecode]

This would create a content source

Clicking on the “ContentSourceWithTypeSharePoint” would open up the content source which we have created using the default settings as below:

This was a simple scenario. Now consider creating the content source of type “Web” which would be

[sourcecode language="xml"]
$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
New-SPEnterpriseSearchCrawlContentSource -SearchApplication $searchapp -Type Web -name ContentSourceWithTypeWeb -StartAddresses http://localhost:5000/
[/sourcecode]

And this would create a content source with type “Web” as below

Here, we can see that the crawl behavior is set to custom and there would be no direct parameters in Powershell to change these options.

So, I recently had a research into this and found out that adding the parameters, MaxPageEnumerationDepth and MaxSiteEnumerationDepth , could bring you these options.

ie, To bring the crawl option to the second option -”

[sourcecode language="xml"]
$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
New-SPEnterpriseSearchCrawlContentSource -SearchApplication $searchapp -Type Web -name ContentSourceWithTypeWeb -StartAddresses http://localhost:5000/ -MaxPageEnumerationDepth 0 -MaxSiteEnumerationDepth 0
[/sourcecode]

This actually means that the Page depth and the Site depth that you expect is zero, which defaultly means that you would be crawling only the first page of your start address and hence this option is selected.

and to bring the crawl option to the first option -”

[sourcecode language="xml"]
$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
New-SPEnterpriseSearchCrawlContentSource -SearchApplication $searchapp -Type Web -name ContentSourceWithTypeWeb -StartAddresses http://localhost:5000/ -MaxSiteEnumerationDepth 0
[/sourcecode]

This actually means that the Site depth that you expect is zero and it can go any pages deep if it is within the server, which definitely means you would be crawling only within the server of the start address.

This post was inspired from a question asked in SharePoint.Stackexchange.com - http://sharepoint.stackexchange.com/questions/27901/how-to-create-a-content-source-of-type-web-with-the-option-only-crawl-within/27908#27908

Export WSP from Farm Solution Store using PowerShell

In this article we will see how we can use PowerShell to export all WSPs from Farm’s solution store.

Open SharePoint 2010 Management Shell and run the following PowerShell cmdlets.

[sourcecode language="powershell"]
foreach($solution in Get-SPSolution)
{
try
{
$filename = $solution.Name;
$solution.SolutionFile.SaveAs("D:\Allaboutmoss\WebParts\$filename")
}
catch
{
Write-Host "-error:$_"-foreground red
}
}
[/sourcecode]
Solution Store :

Exports WSPs :

Custom Signout Page in SharePoint 2010

In this Article we will see how we can set a custom Sign out page for your SharePoint 2010 Web Application. This does not involve dirty tricks like replacing signout.aspx. We will be using Visual Studio to design our sign out page and PowerShell to set our custom page as default sign out page. At the end of this article we will also see how to reset back to default Sign out page.

Fire up Visual Studio and create a Empty SharePoint Project. Add a new Application page. This will be our custom sign out page. Remove the following from your aspx file.

Now lets set out custom sign out page by running this PowerShell cmdlet.

[sourcecode language="powershell"]
Set-SPCustomLayoutsPage -identity "Signout" -RelativePath "/_layouts/CustomSignout/Signout.aspx" -WebApplication "http://yourwebapplication"
[/sourcecode]

To know more about this cmdlet refer this page. Reset IIS and our custom sign out page is ready. Below is a screenshot of our new sign out page.

What if you want to reset back to SharePoint default sign out page? Simple! There is a PowerShell cmdlet for that.

[sourcecode language="powershell"]
Set-SPCustomLayoutsPage -dentity -reset -WebApplication "http://yourwebapplication"
[/sourcecode]

Below is a screenshot of my sign out page after reset.

Post inspired by this question in SharePoint.StackExchange : http://sharepoint.stackexchange.com/questions/14384/signout-aspx-doesnt-seem-to-be-used

Get Solution ID using PowerShell

Here is a quick PowerShell code to get the Solution ID

[sourcecode language="powershell"]
$farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
foreach($currSolution in $farm.Solutions)
{
if($currSolution.Name -eq "SOLUTION NAME")
{
$solutionDetails = "Name: "
$solutionDetails +=$currSolution.Name
$solutionDetails += " ID:"
$solutionDetails += $currSolution.id
$solutionDetails
}
}
[/sourcecode]

How to Add calculated column in SharePoint list programmatically

In this post we will see two methods for adding calculated field in SharePoint list.
Using Object model

[sourcecode language="csharp"]
SPSite site = new SPSite(SPContext.Current.Site.ID);
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPList mylist = web.Lists["MyList"];
mylist.Fields.Add("CalcField", SPFieldType.Calculated, false);
SPFieldCalculated CalcField = (SPFieldCalculated)mylist.Fields["CalcField"];
CalcField.Formula = @"=CONCATENATE("" Calc : "",[Title])";
CalcField.Update();
mylist.Update();
web.AllowUnsafeUpdates = false;[/sourcecode]

Using PowerShell Script

[sourcecode language="powershell"]
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
$SPSite = New-Object Microsoft.SharePoint.SPSite("http://mysite");
$OpenWeb = $SpSite.OpenWeb();
$list=$Openweb.Lists["MyList"]
$list.Fields.Add("CalcField", "Calculated", 0)
$SPField = $list.Fields.GetField("CalcField")
$SPField.Formula="=CONCATENATE([Field1],"" : "",[Field2])"
$SPField.Update()
$OpenWeb.Dispose();
$SPSite.Dispose();
[/sourcecode]

SharePoint Training in Trivandrum

We are glad to Announce that we are starting SharePoint training in Trivandrum, India. If you are in or around Trivandrum and would like to learn SharePoint from Experts, contact us for details.

Classes are normally conducted on weekends so that even working professionals can start learning without affecting their work schedule.

Contact us for Course Details and Introductory offers from allaboutmoss. Don’t forget to spread this word ;-)

Note: We also provide on demand SharePoint training if you are anywhere in India.

Setting a custom 404 error page for SharePoint

In this article we will see how to set a custom error 404 (page not found) error page for SharePoint.

  1. Log on the sharepoint server and access the language folder in layout of IIS (% systemdrive%\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\LAYOUTS\LangID)
    Note: In this path, LangID represents the actual Locale ID of the language that you use. For example, 1033 is the language ID for U.S. English.
  2. Rename the error.html file to error.old.
  3. Create the custom HTML file (pagenotfound.html) and copy it to above folder.
  4. Create a console
  5. application / PowerShell script for assigning the new custom error page for your web application and run / execute it in SharePoint server

C# code for console application

[sourcecode language="csharp"]
Microsoft.SharePoint.Administration.SPWebApplication webapp =
Microsoft.SharePoint.Administration.SPWebApplication.Lookup(new Uri("http://"));
webapp.FileNotFoundPage = "pagenotfound.html";
webapp.Update();
[/sourcecode]

PowerShell code

[sourcecode language="powershell"]
$webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("http://")
$webapp.FileNotFoundPage = "pagenotfound.html"
$webapp.Update()
[/sourcecode]

Note: By default, in Internet Explorer, the Show friendly HTTP error messages setting is turned on and the custom error page may not appear. In this case turn off this setting and try.

3 ways to edit SharePoint page properties

Here comes another post in the series of “3 ways to do….”. Here is how you can edit the properties of a page in Sharepoint

using browser

1. Go to the pages library of the site.
2. Click Checkout menu of the page
3. Click property menu of the page
4. Edit property and save
5. Finally checkin that page
using Object model

[sourcecode language="csharp"]
SPSite site = new SPSite("http://mysite");
SPWeb web = site.OpenWeb();
SPFile file = web.GetFile("./pages/default.aspx");
file.CheckOut();
file.Item["Title"] = "new title";
file.Item.Update();
file.CheckIn("title edited");
[/sourcecode]

using PowerShell

[sourcecode language="powershell"]
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
$site= New-Object Microsoft.SharePoint.SPSite ("http://mysite")
$web=$site.OpenWeb()
$page =  $web.GetFile("./Pages/default.aspx")
$page.CheckOut()
$page.Item["Title"] = "new title"
$page.item.update()
$page.CheckIn("title edited")
[/sourcecode]

Hide a required field in SharePoint list while adding new item

There may be cases when we need to a hide required field while adding a new item to a list but at the same time you may want to show it while editing the item.

The above case looks tricky but it is easily achievable using some extra bit of code or PowerShell

C#

[sourcecode language="csharp"]
string siteUrl = "http://mysite";
SPSite site = new SPSite(siteUrl);
SPWeb web = site.OpenWeb();
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["mylist"];
SPField fldName = list.Fields["Name"];
fldName.ShowInNewForm = false;
fldName.Update();
list.Update();
[/sourcecode]

PowerShell

[sourcecode language="powershell"]
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
$site= New-Object Microsoft.SharePoint.SPSite ("http://d-dev1:1234/gp")
$web=$site.OpenWeb()
$list=$web.Lists["mylist"]
$field = $list.Fields["Name"]
$field.ShowInNewForm = $false
$field.Update()
[/sourcecode]

If you want to hide this field in edit form, you can use below command

[sourcecode language="csharp"]
fldName.ShowInEditForm = false;
[/sourcecode]

If you want to hide this field in display form, you can use below command

[sourcecode language="csharp"]
fldName.ShowInDisplayForm = false;
[/sourcecode]

If you want to hide this field in list settings, you can use below command

[sourcecode language="csharp"]
fldName.ShowInListSettings = false;
[/sourcecode]

If it is not a required filed then you can hide the item using Jquery in client side.

[sourcecode language="Javascript"]
<script type="text/javascript">// <![CDATA[
$(document).ready(function() { $('nobr:contains("Name")').closest('tr').hide(); });
// ]]></script>
[/sourcecode]

3 ways for adding new column in SharePoint list

In this post we will see 3 different methods for adding columns in SharePoint list or document library or discussion board.

using browser
Go to the list which you want to add column
On the page that displays the list, click list’s settings and create columns.
Type a name for the column, choose the column type and click OK.

using Object model

[sourcecode language="csharp"]

SPSite site = new SPSite(siteUrl);
SPWeb web = site.OpenWeb();

site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;

SPList list = web.Lists["mylist"];
SPFieldText fldName = (SPFieldText)list.Fields.CreateNewField(SPFieldType.Text.ToString(), "mycolumn");
fldName.Required = true;
fldName.MaxLength = 50;
list.Fields.Add(fldName);
list.Update();

site.AllowUnsafeUpdates = false;
web.AllowUnsafeUpdates = false;
[/sourcecode]

using PowerShell

[sourcecode language="powershell"]

[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
$site= New-Object Microsoft.SharePoint.SPSite ("http://mysite")
$web=$site.OpenWeb()
$list=$web.Lists["mylist"]
$list.Fields.Add("mycolumn", "Text", 0)
[/sourcecode]