Wednesday, April 20, 2011

C# FileName Remove Special Characters not Allowed in FileName

While generating a filename programatically/dynamically by accepting the filename as user input, it is mandatory to make the raw filename valid by removing the special characters if given.

The following are the special characters which are not allowed in filename.
\/:*?"<>|#%&.{}~
Note: Period(.) is allowed but as filename can't be start or end with it and consecutive periods(.. or ...) are not allowed, considering it as invalid char for filename.
using System;

using System.Linq;

private static string GetValidFileName(string rawFileName)

{

string fileName = rawFileName;

//special chars not allowed in filename

string specialChars = @"\/:*?""<>|#%&.{}~";

//Replace special chars in raw filename with empty spaces to make it valid

Array.ForEach(specialChars.ToCharArray(), specialChar => fileName = fileName.Replace(specialChar, ' '));

fileName = fileName.Replace(" ", string.Empty);//Recommended to remove the empty spaces in filename

return fileName;

}


To know more about filename constraints, look at the detailed post here.

Wednesday, March 30, 2011

SharePoint Error Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack

One of the scenarios of getting this exception is with improper usage of SPSecurity.RunWithElevatedPrivileges.

One of my posts here describes on proper usage of SPSecurity.RunWithElevatedPrivileges to address the same issue.

SharePoint SPSecurity.RunWithElevatedPrivileges Usage

SPSecurity.RunWithElevatedPrivileges is used to run or execute the set of code statements in the context of an Application Pool Account instead of Current Logged in User.

The below code snippet shows the implementation details.
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
{
web.AllowUnSafeUpdates = true;
//Code to edit/delete the file in a library
web.AllowUnsafeUpdates = false;
}
}
});
SPContext.Current.Web can not be used directly with in the RunWithElevatedPrivileges block as the SPWeb object becomes a instance of current logged-in user's context and it gives the below error if tries to update any content in the same Web with READ only access.

Error : Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

To address the issue, a new instance of SPSite and SPWeb should be cerated within the RunWithElevatedPrivileges code block as above.

SharePoint Layout Page for Only Administrator Accessible

SharePoint Layout (Application) pages are accessible by any user who has a least possible access/permissions for any of the sites in the server/farm.

A layout page access can be restricted to only Site Administrator by overriding the RequireSiteAdministrator property of LayoutsPageBase class.

The below code snippet shows sample implementation.
using Microsoft.SharePoint.WebControls;
public class MyLayoutPage : LayoutsPageBase
{
//It mmakes sure only site administrators can access this page
protected override bool RequireSiteAdministrator
{
get
{
return true;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//Code goes here...
}
}
It is just one of the members, look at the post here to know about many other members/methods of LayoutsPageBase class.

SharePoint Manager Tool for Heirarchical View of Content

SharePoint Manager is a very userful and powerful tool for SharePoint developers for reverse engineering, just like .NET Reflector for .Net developers.

SharePoint Manager tool is from the same person who wrote WSPBuilder (another must have tool) in SharePoint Development.

This tool helps the developers to traverse all the content from FARM -> Web Application -> Site Collection -> Web Site -> Libraries -> List Item -> Field etc., in a heirarchical view in a explorer tree view.

It also gives the xml, data of a specific item, properties, caml query of views etc.,

Download the SharePoint Manager 2007/2010.