SharePoint Zip Logo A Black Blade Associates blog. Struggling with SharePoint? We can help.


Blog moved: This blog has moved to http://thingsthatshouldbeeasy.wordpress.com. Go there now to see the new posts.


Thursday, September 10, 2009

How to: Add a folder to a SharePoint list using C#

Unlike adding folders to document libraries, adding folders to lists requires a bit more work in order for the folder to appear in the list view. For example, the following code will add a folder to the list, but the folder will be hidden and not show up in the list view:

   1: //get a reference to the SharePoint objects



   2: using(SPSite reportingSiteCollection = new SPSite(“http://localhost”))



   3: {



   4:     using(SPWeb reportingSite = reportingSiteCollection.OpenWeb())   



   5:     {



   6:         //get a reference to the list



   7:         SPList reportingList = reportingSite.Lists[“Reporting List”];



   8:         



   9:         //the following line will in fact create the folder, but the folder within the list, but there will be no list item associated with the folder, so the folder will not appear in any list views



  10:         reportingList.RootFolder.SubFolders.Add(“New_Folder_Name”); 



  11:     }



  12: }




Instead, you need to add a list item using the SPFileSystemObjectType.Folder enumeration and call the update method on the newly created list item so that the folder the item represents will not only be added to the list but also be visible as a list item:





   1: //get a reference to the SharePoint objects



   2: using(SPSite reportingSiteCollection = new SPSite(“http://localhost”))



   3: {



   4:     using(SPWeb reportingSite = reportingSiteCollection.OpenWeb())



   5:     {



   6:         SPList reportingList = reportingSite.Lists[“Reporting List”];



   7:         



   8:         //the following line will in fact create the folder, but the folder within the list, but there will be no list item associated with the folder, so the folder will not appear in any list views



   9:         SPListItem newFolder = reportingList.Items.Add(reportingList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, “New_Folder_Name”); 



  10:         



  11:         //the following line will associate a list item with the newly created folder and make the folder visible in the list views



  12:         newFolder.Update();



  13:     }



  14: }




Technorati Tags: ,,,

Wednesday, September 09, 2009

GetItems List Query Returns no Items

SharePoint’s SPList class contains the GetItems method which is used to query the contents of a SharePoint list. Rather than iterating through all of the list’s items, the GetItems method allows you to specify a collaborative application markup language (CAML) query to return just the collection of items in which you are interested. It is like sending a SQL query to the SharePoint list. Clearly this is an indispensible method to know about when dealing with any non-trivial list.

One would expect that in order for a call to GetItems to return data, the user invoking the code would need only Read permissions to the list. However, I have run into situations where a user had Read permissions to a list but the call to GetItems returned no data. I had already validated that the query worked by invoking the call to GetItems from another user.

In those situations I could not get a call to GetItems to return any data unless the user had at least Contribute permissions to the SharePoint web site that contained the list. This was the case even if the user had Contribute permissions to the list. Read permission to the SharePoint site or list were insufficient.

I have not yet had time to nail down exactly what circumstances or configuration settings trigger this behavior. If others have seen this behavior and have more insight, I’d love to hear about it. For now, if you have a GetItems call that should be returning data but is not, before spending hours or days  changing around your code, try invoking the call from a user who has Contribute permissions to the site.

Technorati Tags: ,,,,

Tuesday, September 08, 2009

SharePoint WebConfigModifications.Add deletes elements from web.config

I was writing a SharePoint feature that had to register a custom HTTP module. Naturally I started looking at the SPWebConfigModification class and soon had some code that looked like it would add my HTTP module to the httpModules element in the SharePoint web.config file. I compiled, deployed, and activated the feature, and sure enough it worked. My HTTP module was added to the the httpModules element, and the code was even smart enough to detect that if my HTTP module were already registered it would not be registered more than once.

I could have stopped there, but instead I stared getting fancy. I thought: I should check for the existence of the httpModules element itself. I recompiled, redeployed, reactivated the feature, and …. SharePoint died :( I checked the web.config and found that the only HTTP module registered was my HTTP module. All of SharePoint’s default HTTP modules were gone.

I was dumbfounded. How could adding an element end up removing elements? I tried revising my code many different ways but to no avail. I looked at many code samples online that seemed to do the same thing I was doing and comments indicated that those code sample worked.

Finally, a theory occurred to me. There was one difference I could think of between the code samples I saw and my coed: my code was checking for the existence of an element that is created by default when SharePoint creates a new IIS web site, while the online code samples were all checking for the existence of completely custom elements. I did a little digging and found that SharePoint seems use a static web.config file template rather than the SPWebConfigModification infrastructure to generate its baseline web.config files. As such, SPWebConfigModification.SPWebConfigModificationType.EnsureSection will not detect the existence of  element provisioned through the file, because there are no records in the SharePoint hierarchical object store regarding those elements. For your reference, the template web.config file is located at:

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\CONFIG\web.config

Where does that leave a coder who needs to add entries to SharePoint web.config files? Sadly it looks like the code needs to have two different logic paths: one logical path for adding completely custom element that needs to check for each part of the element path being added, and one logical path that only checks for the existence of custom elements within an element path that is part of an element provisioned from the web.config template file referenced above. Using this split logical flow, I’ve generated a code base that can reliably add web.config elements to any part of a SharePoint web.config file.

Expanding your blog readership

In a previous post I was happy the my readership increased to 3000 readers per month, and I promised to share how some of my strategy reaching my next goal: 10,000 readers per month. Here’s some of advise I got from fellow MVPs, some Internet research, and my wife, who was involved in marketing for some big-name advertising agencies…

Post regularly. It is better to post smaller amounts of content on a regular basis, than to post a lot of content sporadically. This will help you to get a reader base that is used to checking back with your blog on a regular basis rather than relying on the search engines to keep bringing you fresh readers with each post. In the past I would write very long articles that would take me a few weeks to complete. Now if I have a long article, I will break it into a multi-part series and publish each part as a separate post.

Read and comment on other blogs. While writing your own content is great, you should also take the time to read other people’s posts on similar topics to your own. Comment on the posts. Many blogging engines allow you to associate you blog Url with the profile you use to make the comment. This will attract readers in two ways:

  1. People who read the other posts may visit your blog if they feel that you have content similar to the post they are already interested and reading.
  2. Search engines will see that other sites are linking to your blog and will increase your blog’s standing in the search results.

Don’t mis-interpret what I’m suggesting. I’m not promoting spamming other people’s blog comments. What I am saying is that you should be active in the online community of which your blog is a part, beyond your own blog.

My wife had told to me that the best time to send out a newsletter or announcement email was on Tuesday or Wednesday morning. Timing the an email that way could dramatically increase the likelihood that it would get read over sending it out on other days or times of days. I thought I would try and apply this same strategy to timing the publishing of my blog posts. No longer would I publish a post at 2:00 AM on a Thursday even if that is when I wrote the post. I would wait until the next Tuesday morning and publish the post by 9 AM local time. That way the new publication is at the top of everyone’s lists of new posts in the morning of a work day (Tuesday) when they’ve had a chance to catch up from the weekend (usually people catch up on Monday).

So how are things going? So far so good. In the past 30 days, my readership has increased to 4,300 visits. That’s an increase of 43%. I’ll keep you posted.

Technorati Tags:

Thursday, September 03, 2009

How to: Choose between an ASP.Net Web Part and a SharePoint Web Part

A colleague of mine recently asked me if the WebPart he was writing should inherit from the ASP.Net WebPart class or the SharePoint WebPart class. He was a bit confused because he had seen a few articles online that talked about the benefits of inheriting from one class or the other.

Some articles recommend using ASP.Net WebPart class for general ASP.Net interoperability while other articles recommend using the SharePoint WebPart class to gain access to its additional features. This MSDN article enumerates the list of features provided exclusively by the Windows SharePoint Services WebPart class:

  • Cross page connections

  • Connections between Web Parts that are outside of a Web Part zone

  • Client-side connections (Web Part Page Services Component)

  • A data caching infrastructure that allows caching to the content database

My colleague was confused. He wanted the ASP.Net interoperability but he wasn’t sure if he would regret not having access to the additional SharePoint WebPart class’ feature.

To my mind, the decision is very simple for most SharePoint developers: use the the SharePoint WebPart class. While the idea of general ASP.Net interoperability is nice, it is not likely to pan out. The reason: if you are writing a web part for SharePoint, you will likely want access to the SharePoint object model to work with list and document data, get SharePoint context, etc…. As soon as you add a reference to the Microsoft.SharePoint.dll assembly to your project and use any of its types, your assembly has already been strongly tied to SharePoint and will not work on a plain ASP.Net web site. If your assembly is already tied to SharePoint, then you may as well get the added functionality of the SharePoint WebPart by inheriting from it.

Technorati Tags: ,,,

Tuesday, September 01, 2009

How to: Get started with SharePoint on the cheap

I’ve seen a lot of articles about how it costs a lot of money to stand up a SharePoint server. I will show that this is simply not the case. In fact, you can stand up a production SharePoint server for less than $1100 USD, including both hardware and software costs. Now, I am specifically refereeing to doing so in production environment, so while you can use MSDN licenses for development, you can not use those here. Still, $1100 for hardware and software… that’s not too bad at all. Let’s see how.

We need to start with the base operating system. SharePoint runs on the Windows Server operating system. Most SKUs of Windows Server require a client access license (CAL), except for Windows Server Web Edition. Windows Server Web Edition retails for $470 USD per server. The only catch is that the 2003 version of Windows Server Web Edition does not allow you to install the database software SharePoint uses for its database. You can of course run the database on a separate SQL Server instance, but that is more costly. Since we want to do SharePoint on the cheap, I will show you another option.

Use SQL Server Express Edition. SQL Server 2005 or 2008 Express Edition is free, $0 USD. It has everything you need to stand up a single-server (not to be confused with Standalone) instance of SharePoint. Combine it with the SQL Server Express with Advanced Features, and you will also get a limited version of Reporting Services and SQL Server Management Studio. There is a nice description here about the licensing terms of Windows Server Web Edition and its restrictions.

Next, we need SharePoint. Since we’re doing this on the cheap, we’re obviously not looking at Microsoft Office SharePoint Server 2007 (MOSS) or Microsoft SharePoint Server 2010 (SharePoint 2010). We’re looking at Windows SharePoint Services 3.0 or 4.0 (WSS). WSS is an add-on for Windows Server. There is no additional server license cost or CAL associated with WSS. Once you pay for Windows Server, WSS is free, $0 USD.

Finally we need some hardware. Thankfully hardware capable of running WSS has really come down in price over the past few years. Windows Web Server edition can handle up to 4 CPUs and 32 GB RAM. while the more quad-processor systems are still on the expensive side, you can pick up a dual-processor, multi-core system with up to 16 GB RAM for a very reasonable price. And, if you go for a refurbished system from a reputable reseller, you can walk away with a steel. My colleague found this advertisement on Buy.com: Dell PowerEdge SC1425 Dual Xeon 3.6GHz 4GB 160GB CD 1U Server with 4 GB RAM onboard, expandable to 12 GB RAM for $320. Max out the RAM with an additional 8 GB for $260 and add an optional 3-year warranty for $50, and you’ve got a very capable SharePoint server for $630 USD.

Here’s the final price list:

Item

Cost

Windows Server 2008 x64 Web Edition

$470

Client Access Licensees

$0

SQL Server 2008 Express Edition with Advanced Features

$0

Windows SharePoint Services V3

$0

1U, Dual Proc server with 12 GB RAM, refurbished

$630

Total

$1100

This setup would make for a great workgroup or even departmental server. it could easily handle the a 500 user load. It’s not quite free SharePoint, but for $1100, it’s pretty darn close.

Technorati Tags: ,,