Posted by: Gabe Hilado in SharePoint,SharePoint Deployment on December 30th, 2009

Using VSeWSS 1.3 with Visual Studio 2008, when you create a new Web Part project, you can edit the Web part properties and descriptions using the WSP View. The typical Solution Explorer in VS 2008 looks like the following when working on a Web part project:

Solution Explorer in VS 2008

Solution Explorer in VS 2008

In order to see the manifest.xml or feature.xml file, you have to look at the “WSP View“. WSP View can be accessed by going to the menu and hitting View–>Other Windows–>WSP View. The WSP View looks like the following:

WSP View

WSP View

The manifest.xml file doesn’t contain “product description” type information. The manifest.xml contains assemblies and features information. The feature.xml file, that on the other hand, start containing “description” type data. Here’s what the feature.xml contents typically look like:


<?xml version="1.0" encoding="utf-8"?>

<Feature Id="cfc5cfdd-62cf-4d98-aeba-e1b38ec6f64f"

             Title="HelloPart"

             Description="A Web part that wants to say hello to you."

             Scope="Site" Version="1.0.0.0" Hidden="FALSE"

             DefaultResourceFile="core" xmlns="http://schemas.microsoft.com/sharepoint/">

  <ElementManifests>

    <ElementManifest Location="HelloPart\HelloPart.xml" />

    <ElementFile Location="HelloPart\HelloPart.webpart" />

  </ElementManifests>

</Feature>

See that Title and Description attributes inside the Feature element? They will get displayed in the Site Features Gallery: 

WP Title and Description in the Features Gallery

WP Title and Description in the Features Gallery

What about the Web part file (in the example I’m using above, the filename is HelloPart.webpart)? What information can be customized and modified here? First, let’s take a look at the contents of the webpart file:

<?xml version="1.0" encoding="utf-8"?>

<webParts>

  <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">

    <metaData>

      <!--

      The following Guid is used as a reference to the web part class,

      and it will be automatically replaced with actual type name at deployment time.

      -->

      <type name="247ef4d4-489d-46d1-a628-8d8daa6267a3" />

      <importErrorMessage>Cannot import HelloPart Web Part.</importErrorMessage>

    </metaData>

    <data>

      <properties>

        <property name="Title" type="string">Gabe's Hello Web Part</property>

        <property name="Description" type="string">HelloPart is a user-friendly Web part....</property>

      </properties>

    </data>

  </webPart>

</webParts>

Inside webPart–>data–>properties section, there are property elements. The first one is the “Title” and the other is the “Description“. The values for “Title” and “Description” contained in the webpart file are what gets displayed in the Web part catalog:

 

WP Title and Desription in the WP Catalog

WP Title and Desription in the WP Catalog

Finally, let’s take a look at the Web part XML file (in the example I used above, the filename is HelloPart.xml). The Web part XML file contains the following:


<?xml version="1.0" encoding="utf-8"?>

<Elements Id="247ef4d4-489d-46d1-a628-8d8daa6267a3" xmlns="http://schemas.microsoft.com/sharepoint/" >

  <Module Name="WebParts" List="113" Url="_catalogs/wp">

    <File Path="HelloPart.webpart" Url="HelloPart.webpart" Type="GhostableInLibrary" />

  </Module>

</Elements>

See how the File element doesn’t have any children? We can put a Property element as a child of the File element. This Property element will contain the “Group” the Web part appears in the catalog. By default, like the in the Web part XML file example above, the Group is not specified and therefore, the Web part gets listed under Miscellaneous Group in the Web Part catalog. If you want the Web part to appear in a group other than Miscellaneous, transform the Web part XML file from the above example to the following:


<?xml version="1.0" encoding="utf-8"?>

<Elements Id="247ef4d4-489d-46d1-a628-8d8daa6267a3" xmlns="http://schemas.microsoft.com/sharepoint/" >

  <Module Name="WebParts" List="113" Url="_catalogs/wp">

        <File Path="HelloPart.webpart" Url="HelloPart.webpart" Type="GhostableInLibrary">

              <Property Name="Group" Value="My Stuff"/>

        </File>

  </Module>

</Elements>

We added the Property element below the File element. The Name attribute of the Property element should have a value of “Group” and the Value attribute is the group name you want the Web part to appear in the catalog. In the example above, after the Web part gets deployed, the Web part appears in a category called “My Stuff“:

Web Part appearing on specified Group

Web Part appearing on specified Group

Posted by: Gabe Hilado in SharePoint on December 18th, 2009

A Web part goes through different states when it gets added to a Web part page. When you first put a Web part on a SharePoint page, you’re typically in “design mode”, where you can modify the Web part properties. After you’re done editing, the SharePoint page and the Web part goes into “browse mode”. You can also preview the Web part in the site collection’s Web part gallery. And don’t forget, SharePoint Designer can also render the Web part in SP Designer’s page editor (Design window).

If you are developing Web parts, you will want to be able to check the state the Web part is in so that you can render content (controls and literals) appropriately. So, how do you check the different states of the Web part? You can check using the following:

(*Note: “this” that you see below is a reference to the Web part class. In other words, the expressions listed below are applicable only when inside the Web part class code.)

  • WebPartManager.GetCurrentWebPartManager(this.Page).DisplayMode.Name – this property is a string with the following possible values:
    • Catalog – this is the value of DisplayMode property when a user initiates the ”edit page” command on a Web part page.
    • Edit – this is the value of DisplayMode property when the user initiates modifying the Web part.
    • Design – this is the value after the OK button is clicked on the Tool Pane (after editing the Web part properties in a Web part page).
    • Browse – browsing the Web part in a normal fashion.
  • this.DesignMode – the value is true when editing the Web part page containing the Web part is being edited in SharePoint Designer.
  • this.IsStandalone – the value is true when the Web part is not contained in a Web part zone. This is typically true when you’re viewing the Web part in preview mode in the site collection Web part gallery.
  • WebPartManager.GetCurrentWebPartManager(this.Page)  – the object returned by this method will be null when the Web part is being previewed (in the site collection Web part gallery)

Checking the state of the Web part is useful when deciding what to render. For example, during regular browsing mode, the Web part will render the normal Web part contents. But if the Web part is being rendered in SharePoint Designer, you might want the Web part to render a “Preview command button” (the way invisble controls like the AJAX ScriptManager or ObjectDataSource look in layout/design editors).