24 March 2009

SharePoint Tool : U2U CAML Query Builder

CAML stands for Collaborative Application Markup Language. It is an XML based query language that is used to retrieve information from a SharePoint site. Writing CAML queries can be a real task. Searching the net for any documentation returns bits and pieces of information.
CAML Query Builder tool saves the headache associated with writing CAML queries (especially if one is new to it).

The downloadable links are available here.

CAML Query Builder Tool for 2007


CAML Query Builder Tool for 2003


Note: The XML generated by the tool is wrapped in <Query></Query> tag. Some of the objects in SharePoint that take CAML query as input require that the <Query></Query> tag be omitted. Since it doesn’t return an error, it can be difficult to debug sometimes.

18 March 2009

MOSS 2007 tip: RunWithElevatedPrivileges to update a list in SharePoint

Recently we had a requirement where we had to create a folder in a document library whenever a listitem was added in another list. Although this seemed simple and the code worked properly in development environment, we started getting errors when we had the application running in production. The reason was that a few users had add rights on one of the lists but had only view rights on the document library in which the folder had to be created.

SharePoint’s SPSecurity has a wonderful but dangerous (if not used properly) method which provides a solution to the above problem.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx gives a detailed description of the method. Point to note here is that the SPSite or SPWeb object needs to be recreated to execute under full control rights. Remember to create the object with the C# using construct so that the object is disposed once the required task is completed.

Feature '00bfea71-e717-4e80-aa17-d0c71b360101' for list template '101' is not installed in this farm

Feature '00bfea71-e717-4e80-aa17-d0c71b360101' for list template '101' is not installed in this farm.  The operation could not be completed.


If you are one of those who have come across this error, then you are not alone. And I know the feeling that goes with it as you see all your web pages throw up random errors which you could make very less meaning of.

I came across this error when I was uninstalling a custom feature that I had written for a site. As complex as the error seemed to be, the solution couldn’t have been much simpler. SharePoint had somehow uninstalled the default Document Library template while uninstalling my custom feature. The solution to this is to re-install the Document Library template which is implemented as a feature in SharePoint.

You should find the feature.xml file in the “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES\DocumentLibrary” location.

Run the stsadm command as shown below

Stsadm –o installfeature –filename C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\TEMPLATE\FEATURES\DocumentLibrary\feature.xml

17 March 2009

Creating folders within an SPList : MOSS 2007

SharePoint allows creation of folders within a list. This has a lot of advantages some of which are mentioned below.

  • Segregation of related list items.
  • Easy management of List operations like add, delete, update and search.
  • Easy management of permissions as related items can be assigned permissions at the folder level.
Usually, there is a requirement of creating these folders dynamically using code.

SPSite oSPSite = new SPSite("http://mysharepointsite");
SPWeb oSPWeb= oSPSite.OpenWeb("mywebsite");

SPList oSPList = oSPWeb.Lists["Country"];
SPListItem oSPListItem = oSPList.Folders.Add(oSPList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, null);

oSPListItem["Name"] = "France";
oSPListItem.Update();

The first parameter of Folders.Add method mentions the level at which the item has to be created. In the above example we specified that the folder should be created at the root level within the list.
The second parameter mentions the type of list item to be created.

12 March 2009

Get Listitems from within folders in a SharePoint List recursively using SPQuery

Usually, a list defined in SharePoint consists of listitems that are stored within folders. One requirement while searching for data using SPQuery object is to get all listitems that match the specified criteria irrespective of where the data is stored in the folder hierarchy of the list.

Consider a list named “Students” that holds records of all students studying at a university. The records are stored within different folders. To get a list of students whose FirstName is “David”, the query would be

SPQuery query = new SPQuery();
query.ViewAttributes = "Scope="Recursive"";
query.Query = "<Where><Eq><FieldRef Name='FirstName'/><Value
Type='Text'>David</Value></Eq></Where>";


The important thing to note here would be the use of the ViewAttributes property of the SPQuery object. The value of ViewAttribues property mentioned above instructs the SPQuery object to search recursively within all folders in the list.

10 March 2009

Using VS2008 to develop SharePoint workflows on 64-bit Windows Operating system

One frustrating issue that we faced was that VS2008 cannot be used to develop SharePoint workflows on 64-bit Windows systems. Selecting any of the SharePoint workflow templates from the menu returns an “Object reference not set to an instance of an object” error. After searching a lot, I came across a bug reported by someone who faced the same problem.

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=325668

The reason mentioned for this bug by Microsoft is a lack of resource and an assurance that it will be solved in the future releases.

Since we already had the setup on a 64-bit machine, the workaround we used was to download sample workflow examples from the internet and modify them according to our requirements.

09 March 2009

Formatting date in ASP.NET gridview

One of our requirements while working with Gridview was formatting a datetime column to display only the date. One of the most important properties to be considered, along with the DataFormatString property, is the HTMLEncode property.

By default, the value for HTMLEncode is set to True. This is done to prevent cross-site scripting attacks and malicious content from being displayed. As a result, format information cannot be passed to the BoundField column.


<asp id="GridView1" runat="server">
<columns>
<asp :BoundField DataField="ExpiryDate"
DataFormatString="{0:MM-dd-yyyy}"
HeaderText="Expiry Date" />
</columns>
</asp>


The HTML code shown below will solve this issue.

<asp id="GridView1" runat="server">
<columns>
<asp :BoundField DataField="ExpiryDate"
DataFormatString="{0:MM-dd-yyyy}"
HtmlEncode="False"
HeaderText="Expiry Date" />
</columns>
</asp>


Hope this saves some valuable time for a few developers.

Welcome to Lazy .NET blogger

If you see this as the first post, then you are probably witnessing history in the making.
I say history because, I see my blog going on to be a source for solutions for all developers like me who break their heads finding solutions which at first glance look simple. Here, you will find solutions to trivial problems in .NET. These are the problems that I faced during various projects that I worked on and had a tough time finding solutions. I hope this will be of help to a lot of developers like me. Do post your comments and queries here and I'll be more than glad to answer them.
Till then, cheers and keep coding!!