Search This Blog

Saturday, March 14, 2009

Identify memory leaks with a Tool

Here is a tool that checks best practices to avoid memory leaks while writing custom code for SharePoint. This will not identify 100% memory leaks but will be much faster to find than manually reviewing the code.

Sunday, March 1, 2009

Configuring Forms Authentication on Windows 2008

Since Win 2008 has IIS 7, Forms authentication can be done without opening the configuration file (web.config) , unlike in the previous version.

Forms authentications is common sceneario for Internet or Extranet scenerio
where external user need to login, possibly to view some premium content which is available only after login.

Here are the steps:
a) Create a site with Non port 80 (say port 20000 or any free port your server). This will be a site with NTLM and Anonymous access enabled. This will be set with the default zone (very important **).

b) Extend the above site at port 80, give it a Host Name say
For right now, this iste is also is a NTLM site with anonymous access enabled.

c) Create the DNS host entries for on your DNS server.
(For temporary workaround and testing you may create c:\windows\system32\etc\drivers\etc\HOSTS file entry. Do not forget to remove this entry)

e) Run ASPNet_RegSQL.exe command from command prompt (from c:\Windows\Microsoft .Net\Framework\2.XXX\..) to create a SQL Server database.
This is assuming your SQL server is already installed on a Separate Server (could be same server also for a Development environment). Let's say the database you created was called MembershipDB.

f) Open IIS 7.0 and select

Create a new ConnectionString say MyConnectionString and the database to to Membership DB above
and Server to the name of the server where DB is created.

g) Double click the Providers option from the right.
g1) From the drop down on the top: Select .Net Roles (or possibly it is already
selected as the default). Click Add from the Actions pane on the right.

Change the Connection string to one aleady created in earlier step.

h) Similary create MyMembershipProvider using type SqlMembershipProvider

i) For Central Admin to be able to add users -
Add a new Connection string - say the same name - MyConnectionString and point to the same Membership database.
j) Create a new provider using WindowsTokenRoleProvider using MyConnectionString

Please note that all the providers and connection string we are adding are automatiocally changing the web.config file. So go ahead and open web.config file of your FBA site and Central admin site. You will these entries have been added by using the above GUI. Ofcourse you can add these directly to web.config also but the above method provides UI for it making it easy and less syntax error prone.


Adding a Content Editor web part programmatically

private void AddContentEditorWebPart(SPLimitedWebPartManager mgr, int zoneIndex)
ContentEditorWebPart cewp = new ContentEditorWebPart();
XmlDocument doc = new XmlDocument();
XmlElement ele = doc.CreateElement("One");
ele.InnerXml = "<![CDATA[<hr style=\"height:1px;color:#939393\"/>]]>";
cewp.Content = ele;
cewp.ChromeType = AspWebParts.PartChromeType.None;
mgr.AddWebPart(cewp, ZoneWebPart, zoneIndex);

In the above example, CEWP is used for adding a horizontal line.

A note of caution: Content Editor web part should not be used if you are using link to images or documents since the absolute references are not updated automatically when content is migrated from one environment to another.

Saturday, February 28, 2009

How to remove a web part causing page to error out

When a web part page or a content page gives an error possibly because of a web part,
we need to remove the web part. To get the current page into maintenance mode
issue the following:




Saturday, February 14, 2009

Custom Content Types and Page Layout in Variation

Variation works good if we are using OOTB content types and page layouts.
However if we have custom content types and page layouts, we will need a
Custom Site Definition with custom content types and reference to page layouts.

Friday, February 6, 2009

Copying documents from Variation Source

Do you need to copy documents from variation source site to other variation sites.
Can it be an Automatic copy of docs?
Yes, provided the following condition is met i.e.

If the URL to the document is in Publishing URL field on Page Layout,
on creating a new page using this Page Layout, add a link to the document and save.

Approve the workflow and magic happens - The document also gets copied to all variation sites from this source variation.
Same is true for Images from Image Library.

Unfortunately List Items will not get copied automatically and we will need custom code to do that.

Monday, January 26, 2009

Automatic Navigation Hierarchy for Content Pages

Simple but powerful...
WSS/MOSS allows an extensible Navigation system.
At times we need to display the hierarchy of pages. For example:
Articles Landing Page (shows a List of all Articles) --> Each Article is a page of its own.

Create a subsite for Articles. Set the home page as Articles Landing Page (from site settings --> Welcome Page). Also set the navigation to show Pages
Now on the quick navigation on left

it will Show the following in Navigation
Site Name (which links to Landing Page)
Individual Article Page1 Title
Individual Artcile Page2 Title

That works Good. How about having Multiple Categories:
Category (each category is a site with Home Page) --> Sub Category (each subcategory is a Page only)

Hence we will see the navigation as:
Category 1 (site with a home page)
Sub Category 1 (page)
Sub Category 2 (page)
Category 2
Sub Category 1
Sub Category 2

So in above case we only created two sites i.e. for Category 1 and Category 2 and the remaining are content pages.

What if there were 3 levels;

Level 1 (site 1 with a Welcome Page)
Level 2 (site 2 with a Welcome/Home Page)
Level 3 (pages below Site 2)

Hope this helps...

Hide the nested controls when a Publishing field value is blank

In a Page Layout, when a publishing field value is blank, it may occupy few pixel space which creates uneven format positions. So overcome this I had created a custom control which supresses the control nested inside this custom control when the value of the field is blank.


Please compile the below code and check for typos since I extracted this from a code doing other things as well. From within the page using SPContext we have access to the data for the same page, hence we can check its value and decide to either render or not render the nested control contents.

Here is how usage would look like:

<custom:HideWhenBlank runat="server" FieldNameToCheck="ImageFieldHere">
.. <...... Field="ImageFieldHere" >


so the value of "ImageFieldHere" will be checked before displaying the image and if it is blank, all the HTML and publishing controls inside the tags will not be parsed and vice versa, i.e. if the image does exist it will be shown.
Also as a note 1) this will work for all data types and not just image types.
2) It is not necessary to inherit from SPSecurityTrimmedControl and you could use other UI base control.
3) In SPD in design mode you will continue to see the nested controls (a check made in above code for design mode), since we would not have access to SPContext at that time. We can the see the end result when the page is shown in browser.

Here is the code:

1 [CLSCompliant(false)]
2 [ParseChildren(false)]
3 [Guid("please put a guid here")]
4 public class CustomTrimming : Microsoft.SharePoint.WebControls.SPSecurityTrimmedControl
5 {
6 public string FieldNameToCheck {get;set;}
7 protected override void Render(HtmlTextWriter writer)
8 {
9 SPListItem item = null;

if (!DesignMode)


10 if (SPContext.Current != null)
11 {
12 item = SPContext.Current.ListItem;
13 }
14 if ((FieldNameToCheck != string.Empty) && (item != null))
15 {
17 if (CheckIfNullOrBlank(item[FieldNameToCheck]) == false)
18 {
19 base.Render(writer);
20 }
21 }
else // to display the nested controls / html when in SharePoint Designer 2007


22 }
24 private bool CheckIfNullOrBlank(object data)
25 {
26 if (data == null || (Convert.ToString(data).Trim() == string.Empty))
27 {
28 return true;
29 }
30 else
31 {
32 return false;
33 }
34 }
36 }

Add the above as a Class file, add SharePoint dll references, assign a Strong Name key. Assuming you are using VseWSS, this will create a WSP file.

Hope this helps.

Tuesday, January 6, 2009

Parser and Lexer

There are 3 types of commonly used lexer/parsers i.e.

LL - Top down recursive parsers. Example: Antlr (
LR - Bottom up parser using tables. Example: Visual Parse++
GLR - Bottom up parser with lot more for handling ambiguities. Example: DParser

For Interactive environments like IDE: An incremental parser is the preferred choice.
Any of the above can come with Incremental parsing features, depends on which one we are using.

For batch conversions and ease of writing Grammars, LL would be the best.
Performance would be better with LR because of tables.
For good error handling and handling ambiguity, GLR would be good.

Moreover these can be with arbitrary lookaheads.
Example: LL(k) - k means infinite lookahead capability.
LL(1) - 1 is only 1 token lookahead capability.

SAP Integration with MOSS

To integrate with SAP, the tool used is the following:

SAP Enterprise services explorer
Version is 1.1

This is Web Services integration with SAP and works with Visual Studio .Net

Search for above on SAP site and you can get a link to download.

XHTML compliance for Rich Text in MOSS

Got this information from a blog item on Telerik (
While the Rad Editor by Telerik is a good tool to save the rich html as XHTML,
but the built in save process in SharePoint will strip of the XHTML.

Solution: Instead of saving the Rich text data to a Rich Text field, save it to text field with multilines. In the Page layout use:

<Rad: field="plaintextfield" />

Where plaintextfield is field defined in the content type as Text field with multilines.

Getting the process id in Windows 2008 Server

While it was cscript iisapp.vbs in Windows 2003 (from \system32),
Windows 2008 has the following command for getting the process id:

appcmd list wp

appcmd is available in \system32\inetsrv
Both the commands are run from command prompt.

Why do we need these:

To attach to the correct W3p process from Visual Studio for debugging your application from Visual Studio .Net

Reading user profiles thru code

Here is a code snippet for reading the user profiles:

using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

UserProfileManager profileManager = new UserProfileManager(ServerContext.GetContext(HttpContext.Current));
foreach (UserProfile profile in profileManager)
/* if we know the field name this may help us to get the value directly instead of using the following loop. Have not tried running the line below so please... */
//string empName = profile["PreferredName"][0] as string;

// The following for loop is used for displaying all the Field Names for profile (and
// can be used for fetching or displaying values also
foreach (KeyValuePair val in profile)
System.Diagnostics.Debug.WriteLine("Key =" + val.Key);