Sunday, August 14, 2011

Access SharePoint list item attachements through code

The code to access the list item attachements is a really simple code.
The attachments of a list item are stored as a collection of files for each list item.
This collection can be access through the Attachments property of the SPListItem object.

An example code is shown below.

// get a reference to the attachments collection
SPAttachmentCollection attachments = listItem.Attachments;
// loop through each file in the collection
foreach(SPFile in attachments)
    // your logic

Friday, August 12, 2011

Update web part properties programatically

Earlier in MOSS 2007, we used SPWebPartCollection objects to modify the web part properties through code. But now it is obsolete.
Now we have a new class called SPLimitedWebPartManager which we can use to do the same thing.

I have created a Visual web part with a button. On click of this button, we will make a page viewer web part show the page.

Below shown is the code

The code will loop through all the web parts in the page where this web part is added and check for a web part with title Page Viewer. When a match is found, it will update the ContentLink property of it and reload the page for the changes to be observed.

You can use the above code for any web part by casting it to an appropriate type

Wednesday, August 10, 2011

The local farm is not accessible

Sometimes you might encounter an error as
The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered
when you try to open Power Shell

To resolve it check the following.

  1. The SQL Server is up and running
  2. The SQL Server is accessible from the machine where you have opened the Power Shell (applicable in a farm environment)
  3. The account used to open the Power Shell has db_owner access on the SharePoint_Config database.
Logging in as the system account and starting the power shell should resolve the issue if the SQL Server is accessible.

Tuesday, August 9, 2011

Create web part pages with custom layouts in SharePoint 2010

SharePoint provides web part pages with eight layouts by default. Unfortunately the process of creating web part pages with custom layouts is a little complex.
In this post i will show you how you can create web part pages with custom layouts.
I have created a component which will deploy an application page which can be used to achieve this.

Go to and download the wsp file with the name CustomSPCF.wsp

Deploy the solution.
This will deploy an application page at /_layouts/customspcf/customspcf.aspx
It will add four web part page layouts at 14\Template\1033\Sts\Doctemp\Smartpgs\CustomSPCF\
and four images corresponding to the layout at 14\Template\Images\CustomSPCF\

The below image shows how the application page will look like

You can customize the application page and replace the deployed custom layouts or add new layouts.
Follow the below steps to replace the exisitng layouts.

1. Go to the applciation page at 14\Templates\Layouts\CustomSPCF\customSPCF.aspx
2. Open it using an editor and replace the options available in it. You can do this by looking for the onetWebPartPageTemplate.

To change the

SingleColumnLayout change the text for the first option, and the files spstd9.aspx and spstd9.gif .
TwoColumnLayout change the text for second option and files spstd10.aspx and spstd10.gif
and so on......

Do an iisreset after you change the existing files so that the changes will be reflected.
I have used the option values from 9 because the values 0-8 are used by the default templates provided by SharePoint 2010.

To add more layouts, add an option and increment the value for the option by one.
Which means if you want to add a new layout, you have to add another line which should look like

<OPTION value="13">New Layout</OPTION>

3. The next step is to add the correspoding layout page
Go to 14\Template\1033\Sts\Doctemp\Smartpgs\CustomSPCF\ and add a file spstd10.aspx

4. The final step is to add the image related to the layout
Go to 14\Template\Images\CustomSPCF\ and add a file spstd10.gif

This completes the process for adding new layouts for web part pages in SharePoint 2010.
Go to your application page again and you should be able to see the newly added option available.

Saturday, August 6, 2011

Information Management Policy settings in SharePoint 2010

Often you will come across situations where you have to do something with your documents after a certain period of time after they have been created/modified.
Information Management Policy settings is where you can configure it. Go to you document library or list settings and click on Information Management Policy Settings

Select the content type for which you want the settings to be applied. This screen will show you all the content types you have enabled for your list or library. For a document library you will see Document and Folder where as for a list you will see Item and Folder by default. 

Click on Document.
In the next screen you will see a list of settings that you can configure like enabling Retention, Labels and Barcodes. But we will only concentrate on the Retention part in this post.
Select Enable Retention. This will show another link called Add Retention Stage

Clicking on Add a Retention Stage will show a pop up.
In the pop you can select when the configured action should be triggered.
You can configure it to run the action after a certain period of time from the time when the document in created or modified or declared as record

Then you can select when the action is triggered. You can configure it to run after specified number of years or months or days

If the retention formula you want is not available in the above list, you can also create custom retention formulae to determine when the action should be triggered. You can so this by extending the
IExpirationFormula class.
Currently the option "Set by custom retention fomula installed on this server" is not enabled as i dont have any custom formulae installed on my SharePoint server.

Then you can select the action to be performed after the configrued period of time.

You can peform various actions as shown in the above screenshot.

Tuesday, August 2, 2011

Create Site Collections from Web Template in SharePoint 2010

I happened to work on a project where we had to create site collection based on the web templates. In SharePoint 2010, when you save a site as template, it is saved as a sandbox solution.
But as we know, sandbox solutions are site collection scoped. So we will see how we can deploy one of these at the farm level and create site collections using the web template.

First we will create a site based on the Team site template, make some changes and save it as template.

I have customized the home page a little to make it look different from the default team site template.
Now go to Site Settings and click on Save site as template

This will save the template to the solution gallery of the site collection
Click on the solution gallery link in the success dialog.
Use the open with explorer feature and copy the wsp.

Open the solution using Visual Studio 2010 now. Create a new SharePoint project of type Import SharePoint Solution and select Deploy as a Farm Solution.

Select the copied wsp from the Browse dialog

In the next dialog that appears you will be able to select the components to import from the wsp. Leave everything select and click on Finish

Visual Studio will give the success message for the import

Now you will see a lot modules and four features in the solution explorer. One of these features contains the files related to the web template.
Double click on each feature to open the properties for that feature.
Generally Feature3.feature contains the files related to the web  tempalte.
Open this feature and change the scope of the feature to Farm.

Then expand the Files in the web template that you see in the above screen. and click on the Elements.xml file.

This will open the Elements.xml file for the web template. Add another parameter to the list of parameters called DisplayCategory. This is the category under which your template will be visible in the site collection creation page.

Change the Subweb parameter to TRUE so that subsites can be created using this template.
Then, double click on the other features to see the properties. Set the IsHidden property to TRUE for all the other features.

Deploy the solution and try creating a new site collection.
This time you will see a new category and a new template along with the default.

Create a new site using the custom template you created.

It is as simple as that. SharePoint 2010 and Visual Studio 2010 have made life easier for the developers haven't they !!!.

Tuesday, July 19, 2011

Site Recycle Bin in SharePoint 2010 SP1

SharePoint 2010 SP1 has brought with it an amazing feature using which site collections administrators can restore sites and subsites which were accidentally deleted. Before SP1 we had to go through a painful process of restoring the database from a backup.

But the admin can simply go to the second level recycle bin and select the subsite that was deleted and restore it just like any other content inside the recycle bin.

In the above picture Sub is the name of the subsite that was deleted.
The site collection administrator can see this in the recycle bin. He can select it, click on Restore Selection and the site is back.
Very simple process, isn't it ??