Problem: Cannot retrieve Propertybag value in SharePoint 2010

I ran into a problem that took me some time to solve and by posting it i might be able to help you save some time.

This is the situation: I was deploying a couple of webparts to SharePoint 2010. The webparts all connected to the same SQL 2008 R2 Databases. So I needed to figure out a way to store the connection string.

There are a couple of options, like hardcoding the connectionstring into the webpart, using custom settings like “modify shared webpart” settings or maybe even use a web.config. For me the best thing to use in this case is the propertybag, it can be configured and changed whenever you like. It can be set on different SharePoint levels:

  • Farm
  • Web application
  • Site collection
  • Site
  • List

So if needed I can even use different connectionstrings for different webparts, located on different levels withing my farm. There are several ways to create values in the property bag:

  • Custom code
  • PowerShell
  • Tooling like (pbs2010)
  • SharePoint Designer

Because I needed a solution I could easily work with and I also want it to be reusable I chose the SharePoint tool pbs 2010, it lets you edit yout propertybag settings from the central admin.

So I installed pbs2010 and added my connectionstring, works like a charm. Ok now I just need a piece of code to read it from the propertybag, Google is mostly your best friend in these cases so I found some code:

 C# |  copy code |? 
// Open SharePoint site 
SPSite siteCollection = new SPSite("http://sharepoint");
SPWeb site = siteCollection.RootWeb;
//Adding SPSite Property
site.Properties.Add("SPSiteKey", "SPSiteValue");
Example: read property from propertybag - site level
// Open SharePoint site
SPSite mySite = new SPSite("http://sharepoint");
SPWeb myWeb = mySite.OpenWeb("Your Web Name")
//Adding SPWeb Property
myWeb.Properties.Add("SPWebKey", "SPWebValue");
//Reading SPWeb Property
string MyValue = myWeb.Properties["SPWebKey"]);

The page I got it from is here. And to my despair, it didn’t work. I got all properties except the one I added with pbs2010. At first I thought it might be a bug in the tool. So I searched the codeplex site and there did not seem to be any other users with the same problem. I tested my theory by adding a propertybag value with SharePoint designer. Again I could read every property with my code except the property added with SharePoint designer. So it could not be the pbs tool, assuming that SharePoint designer did work as it should.

After a lot of searching I found this article it showed me there are two properties to read the propertybag values, SPWeb.Properties and SPWeb.AllProperties. I am not going to do a comparison, but I do know that SPWeb.AllProperties returns all properties and SPWeb.Properties does not return properties added with psb2010 nor properties added by SahrePoint Designer 2010.

My Conclusion, when retreiving values from the propertybag, use SPWeb.AllProperties!

Untill we meet again, Happy SharePointing.

What is PowerShell and why should I use it?

Windows PowerShell is an extendable command shell and scripting language which can be used to manage/administer server environments like Windows Server, Exchange and also SharePoint 2010.

The above PowerShell definition has two things that stick out to me: “extendable” and “scripting language”. Extendable sticks out because it means I can add value to this environment by extending it with my own tools (cmdlets) so I can:

  • make repetitive tasks easier and less tedious.
  • make complex tasks less complex by wrapping several commands together.
  • automate some tasks like e.g. deployment which reduces the risk of human error.
Secondly “scripting language” gets my attention because I am a developer. When doing a bit more research on the scripting language I find that it is based on C# en build on top of and integrated with the .Net framework. So for me as a developer it is easy to learn. 

Pre-PowerShell we used to do all sorts of tasks with all sorts of tools 

  • Central Administration website
  • the SharePoint Products Configuration Wizard
  • stsadm
  • psconfig
These can all be replaced by PowerShell.

Windows PowerShell 2.0 is already in Windows 7 and Server 2008 R2. If you’re not working on Windows 7 or Windows server 2008 R2 you get get instructions here. The containing folder of PowerShell 2.0 is, confusing as it may be, still named 1.0. If you’re not sure, just start up PowerShell and type “get-host”.

Windows PowerShell get-host

Windows PowerShell get-host

The cmdlets used in PowerShell are typically made up out of a verb and a noun as can be seen in the above get-host example. To help you with these cmdlets and create working scripts, Microsoft have created a tool: the Windows PowerShell Command Builder.

For people who don’t like the old-school blue command line console look, or are looking for some more advanced features, there are several third party tools available:

 However, MicroSoft also ships its own Grapihical editor calle PowerShell ISE, it has some nice features:

  • A Command pane for running interactive commands.
  • A Script pane for writing, editing, and running scripts. You can run the entire script or selected lines from the script.
  • A scrollable Output pane that displays a transcript of commands from the Command and Script panes and their results.
  • Up to eight independent Windows PowerShell execution environments in the same window, each with its own Command, Script, and Output panes. This tabbed environment allows you to work on several tasks at the same time.
  • Multiline editing in the Command pane lets you paste multiple lines of code, run them, and then recall them as a unit.
  • A built-in debugger for debugging commands, functions, and scripts. You can set and remove breakpoints, step through your code, check the values of variables, and display a call-stack trace.
  • Customizable features let you adjust the colors, font, and layout.
  • A scriptable object model lets you further customize and extend Windows PowerShell ISE. You can add functionality, including menu items. Also, you can access the session, the files, and the editor to create your own features.
  • Line and column numbers, keyboard shortcuts, tab completion, context-sensitive Help, and Unicode support make Windows PowerShell ISE an efficient and productive environment for using Windows PowerShell. Windows PowerShell even lets you open files by using a drag-and-drop operation.
PowerShell ISE

PowerShell ISE

The only setback, out of the box, it does not support SharePoint, luckily Spence Harbar has found a solution for that.

Last but not least, if you want to use PowerShell for your SharePoint online environment there are some things that need to be done, it will not work out of the box, there is a blogpost by Jeffrey Paarhuis called Scripting SharePoint Online with PowerShell using Client Object Model which will get you up and running.

With that said, I’ll leave it up to you whether you like PowerShell or not and if it’s something to further explore. Untill next time, happy SharePointing!

HowTo: Create a custom SharePoint theme using PowerPoint 2010

SharePoint 2010 has brought us some great new changes, especially when it comes to user friendlyness. One of the new features is the possibility to create a theme using Powerpoint 2010. This way you no longer have to be skilled in Cascading Style Sheets (CSS).

So how does this work? In this tutorial I am creating a new theme, if you want to use an existing theme, open the presentation that holds the theme you want to use and skip the first three steps.

  1. Open a new presentation in PowerPoint 2010.  
  2. Choose a style you like or customize one to you likes. More on how to use and save custom themes in PowerPoint 2010 here.
    Powerpoint Edit Theme

    Powerpoint Edit Theme

  3. Save your PowerPoint document as a theme: File -> Save As -> Office Theme (*.thmx)
    Save as Theme

    Save as Theme

  4. Navigate to your top level site, click Site Actions (upper left) and select Site Settings.
  5. Click ” Themes” located under ” Galleries”.
    SharePoint Themes

    SharePoint Themes

  6. Click “Add New Item”, in the new pop-up window browse to the newly create theme file and click “Ok”.
    Uploading Theme

    Uploading Theme

  7. After the upload you get to name the new theme and give it a meaningful description.
    Naming the uploaded theme

    Naming the uploaded theme

  8. With the above steps, we’re not done yet, we’re halfway, after uploading the new theme, we of course also want to apply it to our site. To do this navigate to the site to which you want to apply the theme.
  9. Click “Site Actions” -> “Site Settings”.
  10. Choose “Site Theme” which is located under “Look and Feel”.
    applying the new theme

    applying the new theme

  11. Select the theme we just uploaded and click  “Preview” if you want to see what your new theme looks like, “Preview” opens a pop-up and allows you to preview your installed theme without applying it to your production environment. If you are happy with the result, close the pop-up and click “Ok”, this makes the changes definitive and we’re done.
    applying the new theme

    applying the new theme

    applying the new theme

    applying the new theme

    Theme applied

    Theme applied

That’s all there is to it. Have fun and happy SharePointing.

SharePoint versions 

  • SharePoint 2010 Foundation
  • SharePoint 2010 Standard Edition
  • SharePoint 2010 Enterprise Edition
  • SharePoint 2010 Online (Office 365 Edition)

Permission level

  • Design

HowTo: Custom SharePoint 2010 Deployment Configuration

A default SharePoint 2010 deployment has seven steps: it runs the pre-deployment command line, packages the WSP file, recycles the application pool, retracts the previous version of the solution, adds the solution, activates all features, and runs the post-deployment command line. This proces is fully customizable.

Creating a custom deployment configuration is not that difficult:

  1. Right click on your SharePoint Project. Click “Properties”. Click “SharePoint”
    SharePoint Deployment Configuration Settings

    SharePoint Deployment Configuration Settings

  2. Click “New” to create a new configuration.
    New Deployment Configuration

    New Deployment Configuration

By default your SharePoint Visual Studio 2010 Project Template comes with two predefined configurations: “Default” and “No Activation”. The two pre-configured configurations cannot be modified. The difference between these two is that “No Activation” does not contain the step “Activate Features”.

Different deployment configurations can be used for deployment to different environments e.g. deployment to a development  server or to a production server. Be sure to select the right configuration for the right environment!

That’s all, maybe until next time, happy Sharepointing!