Monday, January 16, 2012

Add “Change Item Order” to a Custom Link List

I built a SharePoint 2010 Visual Studio solution which included a custom list definition based on a custom content type inherited from the out-of-the-box Links content type.  The out-of-the-box links list contains a “Change Item Order” button in it’s ribbon bar which I needed for my solution (see image below).  I thought I’d get in my custom list by inheriting from the Links content type but that didn’t happen.

image

So I had to get the Change Item order back in my ribbon.  Looking deeper I found that there’s a hidden page called reorder.aspx in _layouts which, with the combination of your list GUID, brings you to a page where you can reorder items.  Try it – go into your list settings and change the page part of the URL from listedit.aspx to reorder.aspx (e.g. http://sitename/_layouts/reorder.aspx?List=[GUID]).

The links list appears to be the only list where this reordering impacts a list view.  You can reorder other lists or libraries using the _layouts/reorder.aspx?List=[GUID] page but it does nothing to change your list views.  Comparing the Links list view settings to other list view settings, you’ll see that you have an option in the Links list to “Allow users to order items” (screenshot below) which you don’t get with other types of lists. 

image

However, the reorder values do stick when you go back to this reorder page for any type of list so in theory you could use this in a CAML query using a hidden field named “order” (e.g. string qry = "<OrderBy><FieldRef Name='Order' /></OrderBy>";) if you were to build a custom content query webpart which needed a link to a sorting interface.

But for my solution, I’m inheriting from the links list content type so the sort works, I just don’t get the option in the ribbon bar.  Chris O’Brien gives a good overview of customizing the ribbon here (Adding ribbon items into existing tabs/groups (ribbon customization part 2).  With Chris’ guide and viewing the source on an OOB Links list view page, I was able to determine that I could get the XML I needed to display the “Change Item Order” in my ribbon by opening the CMDUI.XML and grab the button element XML for Ribbon.ListItem.Actions.ChangeItemOrder (below).

<Button
Id="Ribbon.ListItem.Actions.ChangeItemOrder"
Sequence="20"
Command="ChangeLinkOrder"
Image16by16="/_layouts/$Resources:core,Language;/images/formatmap16x16.png" Image16by16Top="-192" Image16by16Left="-144"
Image32by32="/_layouts/$Resources:core,Language;/images/formatmap32x32.png" Image32by32Top="-192" Image32by32Left="-288"
LabelText="$Resources:core,cui_ButChangeItemOrder;"
ToolTipTitle="$Resources:core,cui_ButChangeItemOrder;"
ToolTipDescription="$Resources:core,cui_STT_ButChangeItemOrder;"
TemplateAlias="o1"/>


Here’s the final XML I used for the element I added to my visual studio solution to get the “Change Item Order” button to appear in my custom link list.  Highlighted in below example:

-- Registrationid=”30099” is my custom list definition's Type value.


-- Location="Ribbon.ListItem.Actions.Controls._children is the location where I want to place my button in the ribbon.


-- I changed the Id for the button from Ribbon.ListItem.Actions.ChangeItemOrder to Ribbon.ListItem.Actions.RibbonSortOrderButton (when viewing source on initial deployment of my custom list, I saw ChangeItemOrder was being trimmed by an OOB JavaScript function so by changing this Id I’m ensuring the OOB JavaScript isn’t trimming it)


-- Sequence="25" is my custom sequence for the order in which this button appears (see Chris O’Brien’s blog for more info).


-- Command="ChangeLinkOrder" is the OOB command which means I don’t have to add a CommandUIHandler.



<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

<!--Change Item order Ribbon-->
<CustomAction
Id="XYZ.Webpart.Links.XYZLinksListDefinition.RibbonSortOrderButton"
Location="CommandUI.Ribbon"
RegistrationId="30099"
RegistrationType="List"
Title="List View Ribbon Customization" >
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition Location="Ribbon.ListItem.Actions.Controls._children">
<Button
Id="Ribbon.ListItem.Actions.RibbonSortOrderButton"
Sequence="25"
Command="ChangeLinkOrder"
Image16by16="/_layouts/$Resources:core,Language;/images/formatmap16x16.png" Image16by16Top="-192" Image16by16Left="-144"
Image32by32="/_layouts/$Resources:core,Language;/images/formatmap32x32.png" Image32by32Top="-192" Image32by32Left="-288"
LabelText="$Resources:core,cui_ButChangeItemOrder;"
ToolTipTitle="$Resources:core,cui_ButChangeItemOrder;"
ToolTipDescription="$Resources:core,cui_STT_ButChangeItemOrder;"
TemplateAlias="o1" />
</CommandUIDefinition>
</CommandUIDefinitions>
</CommandUIExtension>
</CustomAction>
</Elements>


Friday, January 6, 2012

SharePoint 2010 Validate Special Characters

I have a colleague who re-built my custom list based site provisioning solution using a Visual Studio Workflow where I created it using custom SharePoint Designer Workflow actions.  The solution uses the value of what an end-user puts in the “Title” field of the form to generate a URL.  Because the URL can’t contain Special Characters (i.e. ~!@#$%^&*()-+=[]';,./{}\":<>?"), I have actions which validate the column in my solution, using regular expressions, where he didn’t add that to his VS workflow redo.

My colleague left the project recently and not soon after I was alerted about an issue where the site provisioning failed for a user who put a colon and a comma in their Title – to workaround, we simply we had the user resubmit the form with instruction not to use the colon and comma in the Title field but I figured I could fix a reoccurrence by adding the validation on submission.  I didn’t want to jump into visual studio to update his form and workflow and then redeploy the whole thing so I thought “Simple solution…I’ll just add validation to the Title column in the list settings to check for special characters.”  I thought I would have found the formula out there already but had no luck so I’m putting it out now.

So here’s a formula to check for special characters in a text column which you can add via List Settings to the Validation Settings (Figure 1) for the entire list or just to the column’s Column Validation (Figure 2)

=AND(IF(ISERROR(FIND(",",Title)),TRUE),IF(ISERROR(FIND("&",Title)),TRUE),IF(ISERROR(FIND("!",Title)),TRUE),IF(ISERROR(FIND("@",Title)),TRUE),IF(ISERROR(FIND("~",Title)),TRUE),IF(ISERROR(FIND("#",Title)),TRUE),IF(ISERROR(FIND("$",Title)),TRUE),IF(ISERROR(FIND("%",Title)),TRUE),IF(ISERROR(FIND("^",Title)),TRUE),IF(ISERROR(FIND("*",Title)),TRUE),IF(ISERROR(FIND("(",Title)),TRUE),IF(ISERROR(FIND(")",Title)),TRUE),IF(ISERROR(FIND("-",Title)),TRUE),IF(ISERROR(FIND("=",Title)),TRUE),IF(ISERROR(FIND("+",Title)),TRUE),IF(ISERROR(FIND(":",Title)),TRUE),IF(ISERROR(FIND(";",Title)),TRUE),IF(ISERROR(FIND("<",Title)),TRUE),IF(ISERROR(FIND(">",Title)),TRUE),IF(ISERROR(FIND("?",Title)),TRUE),IF(ISERROR(FIND("'",Title)),TRUE),IF(ISERROR(FIND("{",Title)),TRUE),IF(ISERROR(FIND("}",Title)),TRUE),IF(ISERROR(FIND("[",Title)),TRUE),IF(ISERROR(FIND("]",Title)),TRUE),IF(ISERROR(FIND(".",Title)),TRUE),IF(ISERROR(FIND("/",Title)),TRUE),IF(ISERROR(FIND("\",Title)),TRUE),IF(ISERROR(FIND("""",Title)),TRUE))

NOTE:  To validate double quotes, you need to enter double double quotes in your formula (highlighted above).  This sample formula is validating a single word field (i.e., Title) so you don’t need to enclose the field name in brackets ([ ])  where if the field name contains two words you do need the brackets (e.g. [Project Name]).  Also, in SharePoint 2010, the validation formulas can be very similar to data validation formulas used in the past for Microsoft Excel so if your struggling to find a decent sample for a formula search for Microsoft Excel formula examples.

List Settings Validation Options Screenshots:

image

Figure 1 (For overall list – using this method, the validation message appears at top of list item edit form upon failure).

image

Figure 2 (For only the column – using this method, validation message appears below the field in the list item edit form upon failure).