Thursday, December 18, 2008

Protecting static files in ASP.NET

When writing ASP.NET applications and using forms based authentication. Your static files (images, .doc, .htm etc.) are left unprotected. To solve this potentially security problem you can configure a HTTP handler and make the ASP.NET ISAPI filter handle your static files.

Here is a short description on making the static files a little more secure:

ASP.NET Protecting static files with forms authentication

Monday, December 15, 2008

Z-Index and the SharePoint Calendar popup

When styling a SharePoint site you may want to use CSS, DIV etc. to design your site according to your branding needs.

When applying CSS and DIVs to a Publishing site you pretty much have “Full Control” but when mixing a Publishing Site and a Collaboration site you may run into some challenges.

I discovered that my announcement lists did not expand the calendar popup correct. The calendar was cut off by another layer.

ZIndex_001

So I checked the rendering and discovered that the calendar popup is a IFRAME that has it’s z-index to 101. The solution was to set the z-index on my other layers. And decrease the z-index on my layers that where above 101… I also configured the containing layer to have overflow:visible

ZIndex_002

Now it renders correctly.

Friday, December 12, 2008

Modifying MOSSMenu to keep selected item highlighted

The SharePoint Team has released the source code to the SharePoint menu class. (MossMenu). I have modified the source to make the selected item stay selected during a postback.

The solution is a modification of a blog entry in Itay Shakury's blog.

In my sites I do not have menu items pointing to specific views in lists etc. (If I change the default view the menu item still shows the default view) So SharePoint will rewrite my url.

Ex: SharePoint will “UrlRewrite” the following url

http://server/site/Listname/

into

http://server/site/Listname/Forms/My%20Default.aspx

Another example of UrlRewrite is when you specify a folder in a document list. I will not provide a full example url because it contains a lot of characters:

http://server/site/listname/Forms/My%20DefaultView.aspx?RootFolder=http%3a%2f%2fsite...

The good part is that the parameter RootFolder (in my case) is the same as I specified in my menu item. So instead I compare the value of the parameter

Here is the code to handle the UrlRewritings:

(See Itay Shakury's blog on where to insert the code, you must modify the OnMenuItemDataBound function)


if (Page.Request.Url != null)

{

String UrlToCheck = Page.Request.Url.ToString().ToLower();

String UrlOnItem = e.Item.NavigateUrl.ToLower();

String UrlPathParam = String.IsNullOrEmpty(Page.Request["rootfolder"]) ? "" : Page.Request["rootfolder"].ToLower();


Boolean ItemSelected = false;


UrlOnItem = UrlOnItem.Replace(SPContext.Current.Site.RootWeb.Url, "");

UrlToCheck = UrlToCheck.Replace(SPContext.Current.Site.RootWeb.Url, "");

if (UrlPathParam.Length != 0)

UrlPathParam = UrlPathParam.Replace(SPContext.Current.Site.RootWeb.Url, "");


UrlOnItem = Page.Server.UrlDecode(UrlOnItem);

UrlToCheck = Page.Server.UrlDecode(UrlToCheck);

if (UrlPathParam.Length != 0)

UrlPathParam = Page.Server.UrlDecode(UrlPathParam);


if (UrlToCheck == UrlOnItem UrlOnItem == UrlPathParam)

{

ItemSelected = true;

}

else if (UrlToCheck.Contains("/forms/")) // Check if the url is without the forms/pagename

{

UrlToCheck = UrlToCheck.Substring(0, UrlToCheck.IndexOf("/forms/") + 1);

if (UrlToCheck == UrlOnItem)

{

ItemSelected = true;

}


}

e.Item.Selected = ItemSelected;

}

My first blog entry using Live Writer

A colleague showed me this blog editor… It makes it a little more easy to create blog entries.

RegExHelper

(The image is just a test, it is the layout of the Regular expression for gathering ALL images in a HTML document)

This was created with Windows Live Writer Beta

Thursday, December 11, 2008

Regular expression to get ALL images in a page

I have been using a RegEx for a while thar matches all image tags in a HTML document. Suddenly the application one of the applications that is using the function ignored images with "space" in the name.

I had to modify my RegEx a bit... A great tool to use when you need a RegEx Editor is RegEx Buddy. It always helps me in figuring out what I'm doing wrong, and it helps in figuring out expressions gathered from the Internet.

Here is my RegEx that matches all images including src tags with a space in the filename:

<img[^>]*?src\s*=\s*[""']?(<?Filename>[^'"">]+\s*)[ '""][^>]*?>


And the C# function that gets the images into a list


public static List<String> GetImagesFromContent(String Html)

{


List<string> UrlList = new List<string>();


string regExPattern = @"<img[^>]*?src\s*=\s*[""']?(<?Filename>[^'"">]+\s*)[ '""][^>]*?>";


Regex r = new Regex(regExPattern, RegexOptions.IgnoreCase RegexOptions.Singleline);

MatchCollection matches = r.Matches(Html);


foreach (Match m in matches)

{

UrlList.Add(m.Groups["Filename"].Value);

}


return UrlList;


}

Wednesday, December 3, 2008

Include language resources in SharePoint

After reading the blogs Tomblog and Mikhail Dikov on language resources in SharePoint I decided to create a "language aware" version of some SharePoint FieldControls.

In addition I created a function to retrieve the resources that read the resx file if available. If the resourcefile is obsolete (Not published to the App_GlobalResources with ststadm -o copyappbincontent or a feature receiver) The resource will be created from the default resource compiled into the assembly.

Code:

public static String GetResource(String ID)

{

String ResourceString = "";

// Check if the resource exists in the App_Resources folder

// Hint use stsadm -o copyappbincontent to copy the contents from the /CONFIG/Resources directory

// A better way to update the resources is by a feature receiveres

try

{

ResourceString = HttpContext.GetGlobalResourceObject("chandrila_cdlf", ID, SPContext.Current.Web.Locale).ToString();

}

catch

{ }

try

{

if (String.IsNullOrEmpty(ResourceString))

{

// Fall back to the compiled resources

return Resources.chandrila_cdlf.ResourceManager.GetString(ID);

}

else

return ResourceString;

}

catch

{

// If all fails return the ID of the resource that was requested.

return ID;

}

}



Not rocket science, but it helps avoiding strange errors for the SharePoint Adminstrators

Visualize web page performance in Safari

When developing ASP.NET solutions I always check the rendered pages in a lot of browsers. After checking that the output is "OK" I sometimes check the page performance with Fiddler (Not every page and solution is tested...) After downloading Safari I have begun to debug and test performance using Safari.

First off you must enable the "Develop" menuitem, or enable debug mode in Safari. Many posts describes a way that edits the Safari Preferences.plist files. With the current version of Safari this is NOT the way to enable this. It will not make all the utilities work, it displays the Develop menu. But it does not execute the Web Inspector.

The right way to enable the "Develop" menu in Safari (version 3.2.1) is to goto Edit->Preferences and choose Advanced.


Click on the "Show Develop menu in the menubar" and close the window.

If you have done the file file fiddling, just go and toggle the "Show Develop menu in the menubar" off and on again to fix the problem.

OK Now open the "Show Network Timeline" and take a look at the nice graphics displaying the breakdown of your pages.