Archive for the ‘MOSS2007’ Category

Programmatically get the server roles in SharePoint farm

Hi Friends,

While working with Microsoft SharePoint farm we may need to get different server roles it is playing in farm.

Like Application server, Database server, Web Front End server etc.

Following code will show you how we can get the server role using SharePoint object model-

For an example I have created a console application and added reference of ‘Microsoft.SharePoint.dll’ and using


SPFarm spFarm = SPFarm.Local;
SPServerCollection allServers = spFarm.Servers;
if (allServers != null && allServers.Count > 0)
   foreach (SPServer eachServer in allServers)
      switch (eachServer.Role)
	case SPServerRole.WebFrontEnd: Console.WriteLine("WFE:: "+eachServer.Address); break;
	case SPServerRole.Application: Console.WriteLine("APP:: " + eachServer.Address); break;
	case SPServerRole.SingleServer: Console.WriteLine("Single:: " + eachServer.Address); break;
	case SPServerRole.Invalid: Console.WriteLine("Invalid:: " + eachServer.Address); break;

Happy coding…

Get all picture libraries in a web

February 14, 2012 Leave a comment

Hi Friends,

While working in MOSS2007 or in SharePoint 2010 we may come across a situation where we need to get all the picture libraries, all task lists or all KPI list and so on.

This is required in many scenarios like in tool part we want a picture library to be displayed in drop down to be given for selection of one picture library.

In following code I am showing two ways using which you can get all picture libraries from a web.

 using (SPSite site = new SPSite(""))
  using (SPWeb web = site.OpenWeb())
     //we will get all lists in this web
     SPListCollection listCollection = web.Lists;

     // first way is to use the GUID for TemplateFeatureId of picture library
     foreach (SPList eachList in listCollection)
         if (eachList.TemplateFeatureId == new Guid
             ddl.Items.Add(eachList.Title);//here you will get picture libraries

     //second solution will be using the template name
     SPListTemplate Templatetype = SPContext.Current.Web.
                                        ListTemplates["Picture Library"];
     foreach (SPList list in listCollection)
         if (list.BaseTemplate == Templatetype.Type)
             ddl.Items.Add(list.Title);//here you will get picture libraries

Similarly you can use this post to get the other TemplateFeatureId’s and template name. Using this you can similarly get other types lists also.

After “Export to Excel” button click other click/postback events disabled on SharePoint custom webpart

January 16, 2012 6 comments

While working I came across a difficult situation and not able to found the correct reason behind the issue.

I created a custom web part for MOSS2007 with grid view control getting date from list using CAML query and display the data in tabular format. Export to excel functionality required to provide the data in excel to be saved locally from the grid view was one of the requirement I implemented using gridview.RenderControl() method.

I implemented the requirement completely and tested it and as expected it is working perfectly. But if we once clicked the “Export to Excel” button after words other control stops responding like sorting on grid view header, pagination stops posting back on click event.

Then started digging into the issue found the details in one of the telerik grid related post.

Issue is the resubmission of form is stopped by SharePoint to prevent the again and again requesting without the previous request completes. SharePoint maintains a JavaScript flag for this which we need to clear to allow the postback to work after Export to excel.

Solution I used is added following script on the custom web part.Other solutions also available for this problem which explained in  telerik grid related post.

<script type="text/javascript" language="javascript">

//sharepoint postback to work after clicking on export to excel button

if (typeof (_spBodyOnLoadFunctionNames) != 'undefined' &&
                                           _spBodyOnLoadFunctionNames != null)


function supressSubmitWraper() {

_spSuppressFormOnSubmitWrapper = true;



This view is read-only in SharePoint 2007 datasheet view

November 4, 2011 1 comment

Hi Friends,

While working with Microsoft Office SharePoint server 2007 datasheet view will help end users to add or edit the SharePoint list data using Excel like view.

But some time if you found a message at the bottom of the datasheet view as ‘This view is read-only.’ (As shown in below image) and you are not able to add, delete or update the record using the datasheet view.

To resolve this you need to go to the ‘List setting’ section of that same list and select ‘Versioning Settings’ from General Settings section.(As shown in below image)

Now go to ‘Content Approval’ section and set ‘Require content approval for submitted items?’ to No.(As shown in below image)

Happy coding….