Monday, March 21, 2011

How To Add column to all content types and lists in SharePoint

Adding a column to all content types and list in SharePoint is simple, but there are implications that first need to be considered before doing so.  Once completed, the new column will be available in all existing content types in the Site Collection, as well as lists and libraries that exist on SharePoint sites.


Implication:
The implication of this change, is that the process to reverse is not nearly as simple as the process to implement. Removing the column from the content type(s) will not remove the association of the column with lists that previously had the global column.  A custom solution could be developed to automate removal of the column from content types and lists, but the alternative would be to manually remove each instance which could be time consuming in larger, more complex environments.


Process:
Content Types in SharePoint inherit from the Item content type.  If a column is added to this content type (with the “Update all content types inheriting from this type?” option selected), the column will also be added to all existing content types in the site collection.

  1. Navigate to the Content Type Gallery at the root of the site collection, via the site settings page
  2. From under the List Content Types heading, select the “Item” content type.
  3. Press the “Add from new site column”, link to add a new global column, or the add from existing option to add an existing column to all content types.
  4. (If adding a new column) Complete the information about the new column, ensure that the “Update all content types inheriting from this type?” option is selected, and press OK to add the column.

Saturday, March 19, 2011

Dynamic List Filter Menu Using Data View Grouping

This article explains how to create a dynamic filter menu for content in SharePoint.  Methods from the article SharePoint: List Distinct (unique) values from a specific column in a list are used to obtain unique values from a specific column in a SharePoint list to be displayed as each menu item.  When an filter option from the menu is selected, content on the page will be filtered based on the selection.  

For a single menu displaying filter options dynamically, you only need to associate the menu with one column from the main list.  When an option is selected, the browser is redirected to the same page with a query string parameter set to the selected filter parameter using JavaScript.  The main list on the page is then configured to filter contents based on the parameter.

Components:
  • Document Library with custom column "Heading"
  • One Web Part page with the following components:
    • Data View Web Part displaying the actual documents in the library
    • Data View web Part displaying the possible filter options (from the Heading column)


Usage:
  • When a filter option is selected from the menu Data View, the browser will be sent to the current page with the selected option included as a Query String Parameter (using link or JS).
  • The main Data View web part would be configured with a custom parameter obtained from the Query String.  The custom parameter would be used as filter criteria for the Data View.  The result is that when an option from the dynamic menu is selected, the documents listed in the main Data View will be dynamically filtered based on the selected option.


JavaScript Reference:
The “How To” instructions below refer to JavaScript functions used for Query String Manipulation. To view details about the functions referred to below, as well as other functions for hyperlink/query string manipulation, see:

Query String Manipulation using JavaScript
    - Add Query String Parameter using JavaScript - addParameter()

How To:
  1. Create a new Document Library, or use and existing one if required.
  2. Add a custom column to the library (or content type if using custom content types).  In this example the column will have the title “Heading”, and will be used to list menu options, but it can have any title as required.
  3. From the view menu of the document library or the setting page, select the “Create view” option to add a new view.
  4. When viewing the newly created View page, select the “Edit in SharePoint Designer” from the file menu, or manually open the site and page for editing in SharePoint Designer.
  5. Add a second instance of the same Document Library Web Part to the page above the existing.  Set the crome state to None in the Appearance section if required.
  6. Right click on the new Web Part and select Convert to Data View option.
  7. Once a Data View Web Part, open the Sort and Group dialog, and add criteria to group the list by the “Heading” column.
  8. In the design pane of the page, right click on one of the listed items (documents) and remove the row from the table.  This will remove all list items resulting in only the groups remaining.  Remove additional functionality from the groups, such as the expand, contract button/link or css styles.
  9. Format the group value as a hyperlink, with “#” as the value for the href attribute.
  10. Add an “onclick” attribute, with a call to the addParameter() JavaScript function.  The value of the selected filter option and the parameter name is passed to the JavaScript function which adds the parameter to the current url using a redirect.


If there are no existing parameters on the current page url, the easiest method to configure a hyperlink to add a Query String Parameter to the current page is to set the href attribute to the following:

<a href="?main={$fieldvalue}"><xsl:value-of select="$fieldvalue" /></a>

Using the above (simple) method will replace any existing parameters in the current url with the new parameter and value.  For a single dimensional menu, using this method to add Query String parameter and value to use as filter criteria on other Web Part s on the page would be fine.  For multi dimensional menus (main and sub menu), you may need to retain the parameter in the Query String that determines which main/primary heading when adding the sub-heading filter parameter.  In this case, JavaScript can be used to add the new parameter to the existing Query String.  Using JavaScript to manipulate the query string is a better method to use in either case as existing parameters will always be retain, but would obviously require that JavaScript be enabled on client browsers.  




More Resources for SharePoint Development & Customisation:
 

Monday, March 14, 2011

SharePoint: List Distinct (unique) values from a specific column in a list

The following describes two methods to list distinct/unique values from a specific column in a SharePoint list.  An example where this may be useful is a menu system providing filter options for items in a list, using only values that are being used at the time.  


Display Unique Values using Data View Grouping (No Code):
Obtaining a distinct/unique list of values from a specific column in a SharePoint list can be achieved easily using a Data View Web Part, and by applying sort and group criteria to group items.  When grouping (group by) criteria of a Data View web part has been provided, the result is a distinct (unique) list of values from the particular column in the list that will be displayed as group headings.  Once the Data View is listing items grouped by the specified column, you can manually modify (remove cells/rows) the XSL template used to display the table to remove the actual list items from under each group (use Design mode in SPD to easily manipulate the default table layout).

The resulting Data View will list the group names (no items listed), which will be each unique value found in the specified column across all list items in the list.  Using the grouping functionality will ensure that only unique values are returned, as long as you sort by the same field to ensure that items are grouped correctly.  Once you have the list of group headings you can then modify the values displayed to be formatted as hyperlink or another control.  I typically use hyperlinks or buttons with calls to JavaScript functions that add Query String parameters to the current url.
 

Display Unique Values using XSL:
Selecting a list of unique values in a column that contains multiple instances of the same value can also be achieved using an XSL template and the “following” operator.  The following was achieved with the help of this article: http://www.bernzilla.com/item.php?id=333 , and displays a unique list of values from the Title column of items in the list.  


   <xsl:template match="/" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:asp="http://schemas.microsoft.com/ASPNET/20">
       <xsl:variable name="unique-list" select="/dsQueryResponse/Rows/Row[not(@Title=following::Row/@Title)]" />
       <xsl:for-each select="$unique-list">
       <xsl:value-of select="@Title" />
       </xsl:for-each>        
   </xsl:template>
</xsl:stylesheet>


Implementation Examples:
There are numerous circumstances where you need a distinct list of values from a specific column in a list, across all items.  A common scenario may be a dynamic menu system, that provides filter options for content on a page based on existing values in a particular column.

Implementing a dynamic menu system using this technique would involve selecting one or more columns from the main list in SharePoint to be used when listing filter options dynamically.  The menu should list all unique values from a specific field in the list, and be formatted in a way that allows the page to respond to a selection by filtering content.  


The simplest form of this would be a dynamic filter menu displaying options from one field in a list.  When an option is selected, the content on the page (from the associated list) is filtered based on the selected option.  Creating a multi-dimension menu to filter using multiple columns  is also achievable using this technique, where a sub-menu would also exist on the page.  In this case, when an option is selected from the main menu, the sub-menu options are changed (filtered) to show options relevant to the main menu selection.  Once an option is then selected from the sub-menu, the list or library on the page is then filtered using both fields.


More SharePoint Designer Training & Tutorials at SPDTraining.com