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 |? 
01
// Open SharePoint site 
02
SPSite siteCollection = new SPSite("http://sharepoint");
03
SPWeb site = siteCollection.RootWeb;
04
 
05
//Adding SPSite Property
06
site.Properties.Add("SPSiteKey", "SPSiteValue");
07
site.Properties.Update();
08
Example: read property from propertybag - site level
09
// Open SharePoint site
10
SPSite mySite = new SPSite("http://sharepoint");
11
SPWeb myWeb = mySite.OpenWeb("Your Web Name")
12
 
13
//Adding SPWeb Property
14
myWeb.Properties.Add("SPWebKey", "SPWebValue");
15
myWeb.Properties.Update();
16
 
17
//Reading SPWeb Property
18
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!

Tip: Locating the PublicKeyToken of your assembly

Today I ran into a problem concerning a custom webpart. The webpart compiled, so I shipped it ;). But the designtime/compile success was not a guarantee for runtime success. I ran into the following error: Could not load file or assembly ‘xxx, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxx’ or one of its dependencies. The system cannot find the file specified.

So what does this mean? For me the problem was that my libraries were re-signed and in my code there was something like this: <%@ register=”” tagprefix=”CustomControls” namespace=”xxx” assembly=”SharePointControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=123456789″> While in the GAC the assembly had another PublicKeyToken.

When resigning your assemblies, the public key tokens change and an assembly (dll) with the same name is seen as a different version. Which is quite logical because that is what signing with a strong name is meant for.

Next problem is how to get the strong name of my newly signed Assembly, so we can change it in our code. A solution is to deploy your assembly in the GAC and look the PublicKeyToken up in the Assembly folder. There is another solution which is more permament colleague of mine showed it to me:

In Visual Studio, click “Tools” -> “External Tools”. This will open a window in which you can add the command SN as a tool. In the window click “Add” and fill in the fields as seen in the image below. Don’t forget to check the “Use Output window” check box.

PublicKeyToken Tool

PublicKeyToken Tool

When done you can use the tool by selecting the assembly of which you want the PublicKeyToken and clicking the tool  in Visual Studios “Tools” menu. The PublicKeyToken will be displayed in the output window.

That’s all for now folks, happy SharePointing.

Error: The type or namespace name ‘xxx’ could not be found

Today I ran into a small programming related issue. As I was working on a webpart, I was trying to use an UpdatePanel control. So I referenced the System.Web and added the using statements:

  • using System.Web;
  • using System.Web.UI;

But still got me the nasty The type or namespace name ‘UpdatePanel’ could not be found (are you missing a using directive or an assembly reference?) 

Well of course not I added all the needed directives and assemblies. Or didn’t I?

UpdatePanel is an extension, it used to be in the Atlas library which later became Ajax.NET so don’t forget to also reference System.Web.Extensions. This should solve your problems. As it is plain .NET code it is something that works in SharePoint as well as other .NET applications.

Can be a tricky little thing, and of course it doesn’t only go for the update panel but for all the Ajax controls.

So that’s it for now, hope to see you soon, untill then happy SharePointing.

HowTo: Create a teamsite in SharePoint 2010

Creating a teamsite in SharePoint 2010

Very important when working with site templates is knowing what SharePoint out-of-the-box can do for you and very often too much time and effort is spend building that which SharePoint already has. So before you start creating custom templates, check out the existing templates you’ll discover that they often have exactly what you require. That’s why in short I’ll explain what a team site can do for you and how you can create one.

A huge part of SharePoint is about collaboration. Collaboration can be achieved in many ways when using SharePoint. A very popular method to achieve synergy is to use one of the most well known templates that SharePoint 2010 contains: the Team site template. By default the team site contains a shared calendar which you e.g. can use to track important team events. It also contains tasks in which you can keep track of team tasks. You can assign tasks to team members, keep track of status and due date. Finally there is Team Discussion in which you can discuss team items in a very simple manner, it has reply options and also quoted messages.

How to create the teamsite

  1. Navigate to the site which you chose to hold your new teamsite.
  2. In the left upper corner, click “Site Actions”, next click “More Options”. Under “Filter By:” click “Site”.

    Creating a team site

    Creating a team site

  3. Click on the “Team Site” template and fill in an appropriate title. You also get to choose the site url from which you can directly access the new team site.   If you want to save some time, click “More options” it allows you to set permissions and navigation options right away.

    Creating a team site

    Creating a team site

  4. When done, click create and SharePoint will create the site for you.
To get a better understanding of the different SharePoint templates visit this site, it hosts a bunch of movies for all built-in templates and shows you the different possibilities. If you want even more, this site is also a good resource for the different site templates that SharePoint boasts. 
That’s all there is to it, happy SharePointing!
 

SharePoint versions 

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

Permission level

  • Full Control

HowTo: Create a Content Type in SharePoint 2010

Audience

This post is for SharePoint beginners. In a previous post I explained how to create a site column. Site columns can be re-used in lists and they are also used when creating content types. A content type is used to define a SharePoint entity. It can, in a way, be seen as a meta data template which defines which data is stored and how it is stored. A custom content type always inherits from another content type, this can be a built-in type or a previously created custom content type. When creating a content type, you select a content type group and from this group you select the parent content type. The newly created content type inherits all properties from the parent. After this you add site columns and you’re ready to go.

Before you go and start creating content types, assure yourself that there is no built-in content type that fits your needs.

Creating the Content Type:

  1. Navigate to the root of the site collection in which you want to create your content type.
  2. In the upper left corner click “Site Actions” and then click “Site Settings”.
  3. Under “Galleries” click “Site content types”.

    Creating a content type

    Creating a content type

  4. This opens an overview of existing “Built-in” content types as-well as previously created content types. To create a new content type, click “Create” in the upper left corner.

    Creating a content type

    Creating a content type

  5. After clicking “Create”, a window opens in which you can create a new content type. After picking an appropriate name and describing your new content type, you have to pick a parent content type. First pick the appropriate category and then pick the actual parent. Your new content type will inherit all columns from this type. Finally you get to pick the category in which the new content type is placed.

    Creating the new content type

    Creating the new content type

  6. When done, click “OK” to create the new content type. When done you will be taken to the content type details window in which you can see how you content type is built up. This of course is also the place where you add columns to your new content type. How to add these columns can be seen in my article about site columns. You can only add site columns to a content type.
That’s all there is to it. Until we meet again. Happy SharePointing!
 

SharePoint versions 

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

Permission level

  • Design
  • Full Control

HowTo: Set permissions on a list in Sharepoint 2010

If you want to be in control of which user can access certain data you need to set permissions. Individual permissions are kept together in permission levels. So permission levels are in a sense containers which hold sets of permissions which can be assigned to users. By default security permissions are inherited from the container or parent object. you can set unique permissions by breaking this inheritance and assign the permission which you require.

Setting permissions  for a list

  1. Navigate to the list for which you want to set certain permissions.

    Editing list permissions

    Editing list permissions

  2. Click on the list permissions icon, you’ll get an overview of the current permission settings.
  3. To set permissions you first have to break the inheritance. Breaking the inheritance does not remove the current inherited permissions, this setting ensures that new permissions on the parent object will no longer be inherited. Any permissions you don’t want, must be manually deleted, which can be done from the current view. To break inheritance click “Stop Inheriting Permissions”

    Breaking permission inheritance

    Breaking permission inheritance

  4. Next click yes in the confirmation pop-up.

    Confirm breaking permission inheritance

    Confirm breaking permission inheritance

  5. The inheritance is now gone, so new permissions set on the parent will no longer apply to this list.

    Inheritance broken

    Inheritance broken

  6. So again any permissions set until now, will still be apply, you can remove any that no longer apply. The next step is to add the new permissions. Click “Grant Permissions”. This will open a pop-up in which we can assign new permissions.

    Setting a new permission

    Setting a new permission

More specific details on the permission levels can be found here.

That’s all there is to it. Good luck and till next time, until then, happy SharePointing.

SharePoint versions 

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

Permission level

  • Design
  • Full Control

HowTo: Create a Site Column in SharePoint 2010

This post is for SharePoint beginners. When creating custom lists or customizing lists based on the default templates, you often add columns. If you want to re-use these columns you can choose to create site columns, these are re-usable.

Why and when to use site columns

Some columns you add to your lists might be unique, unique in your organisation, or unique in your project. As some columns may be unique, others can have multiple occurrences within you organisation. When columns have more settings and options to it and in that way can become relatively complex, when this occurs it might be handy to re-use these columns. This has several advantages. It saves you time, time in creating them and perhaps time in testing them. It also ensures that every different list, has the same way of displaying and functioning of the site column.

How to create a site column

  1. Navigate to the top level site of the site collection in which you want to use the site column. The site column will only be shared in the site collection and its children.
  2.  In the upper left corner click “Site Actions” and then choose “Site Settings”. Next, under ” Galleries” , click “Site columns”.

    Creating a site column: Menu

    Creating a site column: Menu

  3. On the page that is displayed, a list with all existing site columns is displayed. Click “Create” to create a new site column.

    Creating a site column: Column list

    Creating a site column: Column list 

  4. This opens a page which looks familiar if you have create a list column before, or maybe you’ve seen it reading my post about creating a custom list. When all the properties of the site column are set to fit your needs, click “Ok” to create the site column.

    Creating a site column: Existing site columns

    Creating a site column: Existing site columns 

  5. After creating the site column we of course also want to use it. The first time I tried to, it took me some time to find where I could do that. In the site collection where you created your site column, either create a new list, or use an existing one. Click on the list. In the Ribbon, make sure that under “List Tools”, List is selected (click it). Then, in the Ribbon, click “List Settings”.
  6. Scroll down to “Columns” and click “Add from existing site columns”.

    Adding a site column to a list: Menu

    Adding a site column to a list: Menu 

  7. Add the column to the list and presto, we’re done.

    Adding a site column to a list: Adding the columns

    Adding a site column to a list: Adding the columns 

SharePoint versions 

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

Permission level

  • Design
  • Full Control

More to read

I can imagine that you might want to know even a bit more on site columns, as this article is only about creating a site column. I recommend you read this article, it elaborates about the basics and theory surrounding site columns and content types. If you want to know hot to create a content type, read my article about creating content types.

Thats all folks, for now Happy SharePointing and untill next time.