Pages

Tuesday, October 2, 2012

Disable Multiple File Upload on SharePoint 2007

Sometimes you have to prevent users from uploading multiple files into a document library. For example, you have a custom Edit form and you don't want users to bypass it which is what SharePoint allows them to do when uploading multiple files, even if some fields in a document library are mandatory.

I thought it's an easy task as there is the Upload.aspx file in the Forms folder in each document library. That file has a JavaScript function responsible for multiple file uploads, so you can easily edit that and make it behave as you like. Wrong!

The thing is that Upload.aspx seems to be never used in reality as SharePoint will keep redirecting you to the _layouts/Upload.aspx page, which in its turn can be edited as well, but resides on the server and changes to it would affect the whole farm. This post has more details on why this is happening. 

I've spent some time searching for a solution and the easiest I've found was to amend site's CSS to hide unnecessary controls.

All you have to do is to add the following lines into the file:


.ms-splitbuttondropdown, #ctl00_PlaceHolderMain_ctl01_ctl02_UploadMultipleLink {
display: none !important;
}

The cool thing is that this will not only remove the Upload multiple files link from the upload page, but will also disable the feature within a document library. And it applies on a site level only, so you can leave the rest of your farm intact.

You can achieve the same by other means, but they all about code and amending server files which is not usually acceptable on a large farm.

Monday, October 1, 2012

Changing sub-site URL

A common request to SharePoint administrators in big companies with large collaboration environments is to change the URL of an intranet site. Although not a big job in theory, in practice it becomes a nightmare for anyone looking after a farm with thousands of site collections, not to mention individual sub-sites!

If sites are self-provisioned or there is a request form anyone can use, then it's typical that users choose addresses similar to site titles which, unfortunately, change from time to time, mostly due to organisational changes.


I used to say "no" to such requests and we even have put governance in place to restrict changing URLs, so only business critical changes could be progressed. But recently a colleague of mine found an out of the box feature that some SharePoint administrators may be forgetting about.

The thing is that you can easily change the URL of any sub-site from Site Settings > Title, Description, and Icon. This page gives you the option to change the address online!

Change Sub-Site Address


Changing the address take a second or few as it the actual site is not moved and its GUID remains the same. It's just the address being changed like a property.

This works on 2007, 2010 and 2013. The funny thing is that there is no such option for a site collection and that's why I thought the same is for sub-sites.

Friday, August 17, 2012

InfoPath Regular Expressions (RegEx)

Soon after you start building your first InfoPath forms you realise that you need more advanced validation than that in SharePoint columns. And then you discover InfoPath validation rules that allow you to compare what a user enters into a field with a specific text or number.

Although it's more than enough in the beginning, but soon you find yourself asking for more. And then there are two ways: either end up with writing code behind or use regular expressions (regex). Both are fine, but I personally suggest to avoid developing custom code inside the forms if there are more elegant solutions, out of the box solutions. 

Regex in InfoPath is awesome! It allows you to do so much in one string. For example, this one validates an email address and won't allow anything that doesn't match the pattern:

.+@.+(\.).+

It does look cumbersome at first and you may even start to hate it in case you haven't seen regex before, but that's only until you write your first string that solves a real-world validation problem you've been fighting for days.


How it works

InfoPath itself is very scarce on regex help. The only help I've managed to find inside the program was in the "Data Entry Pattern", i.e. the same window that you use for building your string:



Yes, it shows you how to insert codes for letters, digits, and gives some tips on syntax, but what about general rules, metacharacters or escape characters ? You can google for them and  browse through millions of forums just like I did, but really everything you need is here:



Unlike programming, regex only requires you to understand the syntax - everything else is a matter of combining codes for characters.

I'm not going to explain in detail how to build specific strings, but I'll give a few examples that you may find useful.


Validating domain and username

Say you are developing a site request form which provides new sites to users from specific domains only. The following string will not allow to enter a username which not in the list of specified domains (Domain1, Domain2, etc.) and contains spaces or any special characters except for underscore, dash and dot, which in their turn cannot go first or last.

(DOMAIN1|DOMAIN2|DOMAIN3)\\[A-Za-z0-9]+[_\-\.]?[A-Za-z0-9]+
  • "|" stands for boolean OR operator.
  • "\\" stands for a backslash. You have to type two of them, because one "\" is reserved metacharacter.
  • "[A-Za-z0-9]" - matches any letter or digit.
  • "+" - means that there can be one or more occurrences of the preceding expression, i.e. "[A-Za-z0-9]", which makes an unlimited combination of letters and/or digits.
  • "_" - simply an underscore.
  • "\-" - defines a dash.
  • "\." - defines a dot.
  • "?" - zero or one occurrence of the preceding expression, i.e. "[_\-\.]", so a username can contain zero or one these special characters in between.
  • "+" - one or more occurrence of the preceding expression.



Validating a site name

Another example is useful when you want to validate site names, so users cannot request sites that have any special characters except for underscore and dash, which again cannot go first or last and cannot repeat.

[^_\-][A-Za-z0-9]+[A-Za-z0-9_\-]*[A-Za-z0-9]+

  • "^" - allows to exclude characters that follow it, i.e. won't allow to enter "_" and "-" in the beginning.
  • "*" - one or more occurrences of the previous expression.

Not everything is fine with this regexp - it will still allow you to enter repeating underscores and dashes, i.e. "----". You can go and limit them in the expression or you can add extra validation using other InfoPath rules, for example:



Not as elegant and still prone to issues, i.e. users can enter five or more repeating characters, but it works for me. At least for now :)

Actually, it's a good example showing that regex is powerful, but not ideal and still has some limitations which you can either easily work around or spend hours trying to find a better way. The choice is yours :)



That's it for now, but I'll be definitely coming back to this exciting topic later. I'm sure I'll be using more regular expressions in the future so I hope to share a bit more.