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.



Wednesday, November 26, 2008

Normalize Space....

I'm working on a project that imports data from EPIServer to SharePoint. The solution simply reads a XML file and creates publishing pages, and uploads images based on the content.

The XML structure, and data are provided by a 3rd party vendor, so I don't have control over the content.

The import seemed to work and all the images were uploaded to SharePoint. But when the users began to insert images with formatting attributes in the img tag some spaces were added to the XML source. A quick look at the XML data in Internet Explorer did not reveal anything (Do not look at XML data in IE if you are debugging, Notepad or Notepad ++ is a much better choice).

I tried to make changes to the way XMLReader reads the document, but culd not manage to make the existing XPath query work.

To solve the problem without altering the export routine I had to make a XPath Query that included the entities with, and without the extra spaces.

Here is the XML with the space in the id attribute



The XPath query that were used in previous versions did not match the id when the space were present. A solution culd have been to just add a space in the query, but that did not seem to provide a good solution...



After searching for XML and Whitespace i found the Normalize-Space function. Problem solved!

This XPath query will return the results with or without a space

Monday, November 24, 2008

Enabling language specific help in SharePoint

When using other variations (languages) in SharePoint, the Help files may not have been installed along with the new language. (In fact I believe that the help files are never installed together with the language)

When a user requests help or seeks information the help system will display a message like the image below


The highlighted area indicates the LCID (Locale ID) that is missing its data.

In order to install the missing files you can use a command located in the SharePoint Root + Bin (12\bin) called Hcinstal.exe this application installs the help files associated with your language.

To install a single language use
Hcinstal.exe /act InstallOneHC /loc 1044 /mns MS.OSS
This will install the help files associated with LCID 1044

The utility will return one of three values to indicate the result of the operation, the codes are as follows:
  • 4 - Success
  • 32 - No action taken
  • 256 - operation failed (ex if you specify an LCID that is not installed)

After running the utility, and getting a return output like "Outcome code is: 4" the help window will now have some language specific content...




It took a while before the users reported this issue, as most "proffessionals" the users don't like to read the manuals. But when using "MySite" one link leads direct to the help files.

Welcome!

Hey make a blog, someone said. So I did...

This will be my place on the Internet to share information about the challenges I have encoutered when developing solutions using ASP.NET and/or SharePoint 2007

Regards
Christian