Lars Nielsen's Discoveries

February 21, 2014

Validate UK NHS Number in SharePoint

Filed under: Customization,Development,SharePoint — Lars Nielsen @ 7:50 pm
Tags: , , ,

Just to try to get back to blogging again I thought I would post a function that will validate a UK NHS (National Health Service) number in SharePoint. You can use this on any list or library where you need people to enter a valid NHS Number.

NHS Numbers use a check digit to self-validate. This page describes the format and has a tool to check for a valid number

In SharePoint you can specify a validation formula for a column.  So in my example I created a column on a list called NHSNumber and in screen that defines the column, I set the maximum number of characters to be 10, and in the Column Validation section I used the following formula. You can basically just copy and paste this, and substitute your column name for “NHSNumber” in the formula

=IF(LEN(NHSNumber)=10,
	IF(NOT(ISERROR(NHSNumber+1)),
		OR((11-(MOD((LEFT(NHSNumber,1)*10+
				RIGHT(LEFT(NHSNumber,2),1)*9+
				RIGHT(LEFT(NHSNumber,3),1)*8+
				RIGHT(LEFT(NHSNumber,4),1)*7+
				RIGHT(LEFT(NHSNumber,5),1)*6+
				RIGHT(LEFT(NHSNumber,6),1)*5+
				RIGHT(LEFT(NHSNumber,7),1)*4+
				RIGHT(LEFT(NHSNumber,8),1)*3+
				RIGHT(LEFT(NHSNumber,9),1)*2),11)))=RIGHT(NHSNumber,1)*1,
			AND(
				((MOD((LEFT(NHSNumber,1)*10+
				RIGHT(LEFT(NHSNumber,2),1)*9+
				RIGHT(LEFT(NHSNumber,3),1)*8+
				RIGHT(LEFT(NHSNumber,4),1)*7+
				RIGHT(LEFT(NHSNumber,5),1)*6+
				RIGHT(LEFT(NHSNumber,6),1)*5+
				RIGHT(LEFT(NHSNumber,7),1)*4+
				RIGHT(LEFT(NHSNumber,8),1)*3+
				RIGHT(LEFT(NHSNumber,9),1)*2),11)))=0,
				RIGHT(NHSNumber,1)*1=0)
		)
	)
)

The formula above also checks that the string has 10 characters and that all the characters are digits, before calculating the check digit.

It’s not easy to test validation code, but as a check I used this great post to set up a spreadsheet column to  validate a string of text as an NHS Numbers.  I checked that my SharePoint formula is giving the same results as the spreadsheet formula, using several hundred randomly-generated 10-digit strings.  It all seems to work OK, but if you spot any errors please let me know.

March 6, 2011

SharePoint Built In Fields

Filed under: Development,Good Practice,SharePoint — Lars Nielsen @ 6:19 pm
Tags: ,

Instead of using hard-coded strings for the names of the built in fields in SharePoint, you can use the object model to get the GUID of the field.  So instead of doing this:


SPListItem i;

// Get a list item into i

string moderationStatus = i["_ModerationStatus"] ;

You can do this:


SPListItem i;

// Get a list item into i

string moderationStatus = i[SPBuiltInFieldId._ModerationStatus] ;

This will get you a reference to the correct field using its GUID rather than its name.  The full list of built in fields is in the SPBuiltInFieldId documentation.  The SharePoint Kings blog also has an explanation of this class and also SPBuiltInContentTypeId.  There are a couple of problems with the fields to watch out for:

  • If you want to get the account who created an item, use the field SPBuiltInFieldId.Author not SPBuiltInFields.Created_x0020_By
  • If you want to get the account that last modified an item, use SPBuiltInFieldId.Editor not SPBuiltInFieldId.Modified_x0020_By

I’m not sure when SPBuiltInFields.Created_x0020_By would work but in my experience on a regular list, it’s always empty so I used SPBuiltInFieldId.Author instead.  So for example to get the account that created a list item as an SPUser object (so you can get the name or their email address) use code like this:


SPListItem item;

// Set the value of item somehow

SPFieldUser createdByField = (SPFieldUser)item.Fields[SPBuiltInFieldId.Author];

SPFieldUserValue createdByUserValue = (SPFieldUserValue) createdByField.GetFieldValue(item[SPBuiltInFieldId.Author].ToString());

SPUser user = createdByUserValue.User;

January 31, 2011

Custom EditForm.aspx page error “The page you have selected does not contain a reference to the correct SharePoint list”

Filed under: Customization,SharePoint,Troubleshooting — Lars Nielsen @ 10:52 pm
Tags: ,

If you are trying to create a custom property editor screen for a document library (to replace the default EditForm.aspx page), you might follow Jan Tielens instructions on how to do it for a normal list.  That works fine for a normal list, but when you try to do the same with a document library, when you click the Apply button in the Supporting Files tab in SharePoint Designer you’ll get an error:

The page you have selected does not contain a reference to the correct SharePoint list. Please select another page

To avoid this you need to make sure that the original ListForm web part is still there in the markup on the page, but you make it invisible and add a custom list form web part above it.  But you also need to remember that how to create your custom edit page is important as well.  You might think the easy way is to do this:

  • Open the site in SharePoint Designer
  • Select the EditForm.aspx in the Forms folder of the document library
  • Right click – Copy
  • Select the Forms folder
  • Right click – Paste

That will create a copy of the EditForm.aspx file that you can rename and work with but you keep getting the error above when you try to apply it to the document library.  So instead create the custom editor page  like this:

  • Open in SharePoint Designer
  • Open the EditForm.aspx in the Forms folder of the document library
  • Save it with a different filename

Now start editing your new page.  Remember not to delete the existing ListForm web part, just make it invisible.

June 15, 2010

Performance code tips for SharePoint

Filed under: Development,Good Practice,SharePoint — Lars Nielsen @ 7:02 pm
Tags: ,

I thought I’d collect together some tips on how to improve the performance of code in SharePoint and how to avoid some traps which might cause code to run slowly or break in production systems.

1) Accessing Lists

You need to be careful when working with SharePoint lists – often code that might work fine on a dev machine with small lists won’t work so well when you deploy it to a production environment where the lists are much bigger.  In particular, watch out for the right way and wrong way to loop through the items in a list.  Never loop through the SPList.Items object, because SharePoint will query the database each time within the loop.  Instead set a new SPListItemCollection variable to point to SPList.Items and then loop through the new variable.   This will cut it down to just one query to get all the items in one go, rather than one at a time.

Even better is to the GetList() method of the SPWeb to get a reference to the list.  This is less demanding in terms of database access and also you can test if the list is there by trapping a FileNotFoundException which is thrown by the method if the list does not exist.  Use code like this to get a reference to the list:


string listName = "My List"

try
{
string s = _currentSPContext.Site.ServerRelativeUrl; // Site collection root Url
if (!string.IsNullOrEmpty(s))
{
          string siteCollectionRelativeUrl = s.TrimEnd('/');
SPList MyList = _currentSPContext.Site.RootWeb.GetList(siteCollectionRelativeUrl + "/Lists/" + listName);
.....
}
}

catch (FileNotFoundException)
{
    .....
}

2) Counting items in a list

In the same way, it’s better to use SPList.ItemCount rather than SPList.Items.Count, again to prevent multiple queries on the database.  Although the results do differ if you have folders in the list – SPList.ItemCount counts a folder as one item, whereas SPList.Items.Count ignores the folders.

Here are a load more great best practice advice

[Update: 28/3/2012]

I just found this great post with lots of detail and advice on good and bad ways to work with lists in the object model.  It’s quite old now but I think it’s still valid for SharePoint 2010

Create a free website or blog at WordPress.com.