Wednesday, December 3, 2014

SharePoint File and Folder Name validation


Here is a quick sample on how you can validate a file or folder name before creating/uploading it in SharePoint.
Scenario in which I used this code block when I am creating a folder hierarchy in SharePoint site (on-premises or online) based on a database query and migrating files.
Based on the KB 905231, there are following characters are not allowed in the file/folder name for SharePoint.
  • Tilde (~)
  • Number sign (#)
  • Percent (%)
  • Ampersand (&)
  • Asterisk (*)
  • Braces ({ })
  • Backslash (\)
  • Colon (:)
  • Angle brackets (< >)
  • Question mark (?)
  • Slash (/)
  • Plus sign (+)
  • Pipe (|)
  • Quotation mark (")
Along with those, you have to check below conditions also:
  • You cannot use the period character consecutively in the middle of a file name.
  • You cannot use the period character at the end of a file name.
  • You cannot start a file name by using the period character.
  • If you use an underscore character (_) at the beginning of a file name, the file will be a hidden file.
Extra conditions:
In addition, file names and folder names may not end with any of the following strings:
  • .files
  • _files
  • -Dateien
  • _fichiers
  • _bestanden
  • _file
  • _archivos
  • -filer
  • _tiedostot
  • _pliki
  • _soubory
  • _elemei
  • _ficheiros
  • _arquivos
  • _dosyalar
  • _datoteke
  • _fitxers
  • _failid
  • _fails
  • _bylos
  • _fajlovi
  • _fitxategiak
In My code block, I am not considering the extra conditions.
You can pass the required file/folder name which need to be validated to the function, and function will let you know if that is a valid name.

public static bool NameValidate(string strName)
{
bool isValidFileName = true;
var match = (new Regex("[ ~ # % & * { } : < > ? / + | \" \\\\ ]|\\.\\.|^\\.|^[_]|\\.$")).IsMatch(strName);
if (match)
{
//Invalid file name.
isValidFileName = false;
}
else
{
    //Valid file name.
}
return isValidFileName;
}

Else you can also use below options if you have to replace each occurrence of characters and fix the name –
char[] filenameChars = fileORFolderName.ToCharArray();
     foreach (char c in filenameChars)
     {
         if (!SPEncode.IsLegalCharInUrl(c))
fileORFolderName = fileORFolderName.Replace(c.ToString(), "");
     }

 In above the reg ex can be broken as:
[ ~ # % & * { } : < > ? / + | \" \\\\ ]Any one of the chars anywhere in the name (last \\\\ represent one backslash \)
\\.\\.. .( double Dot) consecutive dot occurring in the name
^\\.. (Dot) at the start of name
^[_]_(underscore) in the beginning of name
\\.$. (Dot) at the end of name




Feel free to share your comments.

Saturday, April 28, 2012

FAST Search 2010 on Taxonomy

Recently during my work, I got to build the query string for FAST Search 2010 for SP 2010. The refiner was the managed metadata column.
When we compare the enterprise search  result query string and FAST search query string, we see that FAST search has alphanumeric string after the "r" parameter.
Enterprise search has string as : r=%22owstaxIdColName%22%3D%230b37b7b2%2D0079%2D402b%2D8342%2D3aac932037ec%3A%22EMEA%22, which is : r="owstaxIdColName"=#0b37b7b2-0079-402b-8342-3aac932037ec:"EMEA".
While when same query string come as : r=%22owstaxIdColName%22%3D
AQRFTUVBCG93c3RheGlkABgDXiJvd3N0YXhpZFRlc3RUYWcjMGIzN2I3YjItMDA3OS00MDJiLTgzNDItM2FhYzkzMjAzN2VjIiQ%3D in FAST Search 2010 for SP.
Now challenge is how to build the query string in FSAT search. For this I referred the referenced articles and got a lot of ideas from Mikael.
I downloaded the attached refinementtokenencoder.cs. In the code, there is a refinement class.When you pass the existing alphanumeric string, you will get the components in the string as.
refinement.ManagedPropertyName.
refinement.RefinementName.
refinement.RefinementValue.
Since you know what was selected refiner, and you got the components of string. Now you pass your own components and use RefinementTokenEncoder.GenerateToken(refinement) to get the base64 string for FAST Search 2010.
I have tried this and worked perfectly for one refinement.it is easier than writting long FQL query and customizing core search web part to enable FQL for managed metadata column.

This can help when we have associated taxonomy with documents and just want to filter the documents associated with taxonomy. No need to enter any search keyword also.

I will put next blog on multiple refinement.

Reference:
http://msdn.microsoft.com/en-us/library/ff625182.aspx
http://techmikael.blogspot.in/2011/08/creating-refinement-query-parameter-for.html

Thursday, June 30, 2011

Creating SP 2010 Sites using PowerShell

We all know PowerShell is recommended command line tool for SharePoint 2010. But PowerShell does not support the same style as the UI supports.
I ran in to problem, when I was creating sites using PowerShell. After creating sites I was not able to see the default groups (Members, Visitors and Owners) for the site. But when you create site from UI, you have all those available.
I figured out that, we need to execute one more command in order to get that. The steps can be as listed below:
1. Create Site, either using New-SPSite or simple .NET style code to add site to web application $WebApp.Sites.Add(params....).
2. After that execute command as :
$CurrentWeb.CreateDefaultAssociatedGroups($PrimaryOwnerLogin,
$SecondaryOwnerLogin,
$CurrentWebTitle)

It will create the groups with the current site title for e.g. MyProjects Members.

Friday, June 24, 2011

SharePoint 2010 with search scope on managed path

I came across this while trying to restrict my search on a specific managed path. I tried a lot options and at last the tricky configuration did the work. The steps to do so can be as below:

1. Go to central admin site of your farm.
2. Navigate Application Management --> Manage Service Connections.
3. Locate Search Service Application. Select it by click it in the same row. (as usual step in SP 2010).
4. From left quick launch, navigate to scopes.
5. Create New Scope with your preferred title, description and specify your custom search result page (ex - salesresults.aspx)
6. Create rule as :
select Scope Rule Type as Web Address,
select Web address as http:/// (ex - http://myserver/sales)
select behavior as include.
7. Now go to your search center, where you have created your custom search tab.
8. Go to the result page (which is created using search results page layout) for that custom tab.
9. Edit page and locate search core results web part on bottom of page.
10. Edit the web part.
11. Expand location properties. In the Scopes text box, enter your scope name which is created in step 5 above.
12. save web part settings and save page. checkin the page and publish it.
13. when you search on this custom tab, on refiner list under site, you can only sites available under the managed path.

Done.