Friday 15 November 2013

Chrome Type for Web Parts in SharePoint 2010

Change Chrome type property of a webpart through Visual studio



 Chrome is the surrounding area around a web part that gives you a border, a title for the web part, and other aesthetics…if you want them. The problem is, often times when you are dealing with a public website, you do not want to show the chrome around a web part just because you have more control of your site’s look and feel when all of your visible elements are created from within the web part content.
There are a couple ways to disable chrome for a web part within SharePoint, each coming with a bit of a caveat. The first is to simply change the web part properties for a particular web part setting the chrome type to the desired case, which is ‘None’ in this case.

Chrome


We can also do the same thing in our web part definition (.webpart file), as shown below:
<?xml version="1.0" encoding="utf-8"?>
  <webParts>
    < webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
      < metaData>
          < type name="My.Namespace.and.type, $SharePoint.Project.AssemblyFullName$" />
          < importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
      < /metaData>
      < data>
        < properties>
            < property name="Title" type="string">My Web Part</property>
            < property name="Description" type="string">My Web Part’s description</property>
            < property name="ChromeType" type="chrometype">None</property>
        < /properties>
      < /data>
    < /webPart>
</webParts>


The problem with these two solutions is that you have to change every web part’s properties to set the chrome type, which is quite inefficient. The other way of doing this is a more global solution. Chrome type can be changed at the page layout level as well. Every web part zone has a PartChromeType property that can be set to one the defined values – in this case, we will choose ‘None’ for our publically facing website.

<WebPartPages:WebPartZone id="wpzMyZone" runat="server" title="My Zone" PartChromeType="None">

There is one big gotcha with all of this. Whenever a web part is placed on the page, the chrome type for that web part is written to the page metadata deep within the bowels of SharePoint. So changing the web part definition or changing the global layout will have no effect because it the web part chrome type has already been determined. So if you change the chrome type after a web part has been added to a page, you are left with two options: change it manually in the web part properties, or remove and re-add the web part to the page. 

No comments:

Post a Comment