Thursday, May 14, 2009

Creating a field in a List and getting the error: "Index was outside the bounds of the array"

Today, while I was code reviewing a new release of our rating field type I got a weird error message...

We added a new feature that adds 2 read only fields with the total number of ratings and number of comments.

Code example for addnig new fields was simple:
if (!list.Fields.ContainsField(ratingField.FieldName_NumOfCommentsInternalName))
{
list.Fields.Add(ratingField.FieldName_NumOfCommentsDecodedName, SPFieldType.Number, false);
//Some more logic here
}


Pretty simple, only when this code runs this time (3rd unit testing sessions before release to customer review) suddenly I get the error message above "Index was outside the bounds of the array".

Well... I ran it in debug few time, scratching my head few times more, but nothing - same error every time!

OK, time for drilling into SharePoint code I guess...
The exception stack trace was as following:
at Microsoft.SharePoint.Utilities.SPStringUtility.EscapedCodeToLower(String str)
at Microsoft.SharePoint.SPFieldCollection.FixFieldV3Attrs(XmlDocument xd, XmlElement xe, Guid& fid, String& name)
at Microsoft.SharePoint.SPFieldCollection.TranslateFieldSchema(String schemaXml, Guid& fid, String& strDisp, String& strStaticNew)
at Microsoft.SharePoint.SPFieldCollection.AddFieldAsXmlInternal(String schemaXml, Boolean addToDefaultView, SPAddFieldOptions op)
at Microsoft.SharePoint.SPFieldCollection.AddFieldAsXml(String schemaXml, Boolean addToDefaultView, SPAddFieldOptions op)
at Microsoft.SharePoint.SPFieldCollection.AddFieldAsXml(String strXml)
at Microsoft.SharePoint.SPFieldCollection.AddInternal(String strDisplayName, SPFieldType type, Boolean bRequired, Boolean bCompactName, Guid lookupListId, Guid lookupWebId, StringCollection choices)
at Microsoft.SharePoint.SPFieldCollection.Add(String strDisplayName, SPFieldType type, Boolean bRequired, Boolean bCompactName, StringCollection choices)
at Microsoft.SharePoint.SPFieldCollection.Add(String strDisplayName, SPFieldType type, Boolean bRequired)
at KWizCom.SharePoint.Rating.ListRatingUtilities.CreateRatingCountFieldsInList(SPWeb web, SPList list, RatingField ratingField)


Revealing that the exception was raised from a method called:
Microsoft.SharePoint.Utilities.SPStringUtility.EscapedCodeToLower(String str)

So far - makes sense... string parsing is always the number one cause for index bound exceptions, right?

So, I wanted to check what was wrong with the field name I was trying to create...
ratingField.FieldName_NumOfCommentsDecodedName "2Rating Site Column_x" string


See that my field name ends with "_x"...

The code I extracted from "EscapedCodeToLower" had the following line in it:
if (((str[i] == '_') && (i < (str.Length - 1))) && (str[i + 1] == 'x'))


Well, now everything was clear to me.

Microsoft uses "XmlConvert.EncodeName" in order to encode fields internal names in SharePoint. This encoding method replaces illegal characters with "_x0020_" for example. Apparently this method looks up every "_" that was followed by "x" and assumes it is a token it needs to decode... But what happens if my text ends with "_x"?

Aha! You get your "Index was outside the bounds of the array" exception!

So, I guess now I have to add a check to my code. I Doubt anyone would notice it or bother to fix it.

Well, I googled that error message regarding SPList.Fields.Add and got nothing - hopefully this will help someone in need in the future...

Shai.

Monday, May 11, 2009

How to backup and restore a website with Variations

The article below was written by Armine Vardanyan, a SharePoint SharePoint Implementer for KWizCom Professional Services. Armine explains step by step how to backup and restore a website with Variations

************************************

When you backup a SharePoint site that uses the Variations feature, the variation jobs you created are not automatically backed up. Thus, before restoring your site to a new web application, you will need to create Variation labels and the needed hierarchy so that the variations jobs will be created automatically. Once this task is completed you will be able to restore your site safely, without losing any information.


Here's how to correctly backup and restore your site, with its Variation labels:

1. You need to backup the web application by using stsadm(recommended):
stsadm -o backup -url
http://portalname/ -filename myBackup.bckSee How to copy site collection from one server to anther (Backup and Restore).
2. Create a new web application from the “SharePoint Central Administration” where you want to restore the backup and create the site collection. (/).










3. Go to your created web-site.
4. Go to “Site Settings” > Site Settings > Modify All Site Settings
5. Under the “Site Collection Administration” section click on “Variation labels”






6. Click on New Label and create your Variation labels (same as when you created them in the site you backed-up)
7. Click on Create Hierarchies.





8. After the hierarchies were created you can restore your backup and overwrite by:
stsadm -o restore -url
http://newportalname/ -filename MyBackup.bck –overwrite

Not so hard right?

Monday, May 4, 2009

Problems accessing MOSS with IE8?

The article below was written by Armine Vardanyan, a SharePoint SharePoint Implementer for KWizCom Professional Services.

****************************

Problems accessing MOSS with IE8?

by Armine Vardanyan

Ever try to access your site through IE8 (Internet Explorer 8), enter your user name and password in the following window:






















Only to see the window pop up again? You click OK and again it comes up? And again?!
Suddenly you get the following error:

















Your server is probably running IIS 5.1 or IIS 6…

There are two ways you can fix this problem:

• Method 1: Specify host names
• Method 2: Disable the loopback check



Method 1: Specify host names
Note: We recommend that you use this method.

To specify the host names that are mapped to the loopback address and can connect to Web sites on your computer, follow these steps:

1. Click Start, click Run, type regedit, and then click OK.
2. In Registry Editor, locate and then click the following registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0
3. Right-click MSV1_0, point to New, and then click Multi-String Value.
4. Type BackConnectionHostNames, and then press ENTER.
5. Right-click BackConnectionHostNames, and then click Modify.
6. In the Value data box, type the host name or the host names for the sites that are on the local computer, and then click OK.
7. Quit Registry Editor, and then restart the IISAdmin service.

or using Method 2:
Disable the loopback check

Follow these steps to disable the loopback check:

1. Click Start, click Run, type regedit, and then click OK.
2. In Registry Editor, locate and then click the following registry key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa
3. Right-click Lsa, point to New, and then click DWORD Value.
4. Type DisableLoopbackCheck, and then press ENTER.
5. Right-click DisableLoopbackCheck, and then click Modify.
6. In the Value data box, type 1, and then click OK.
7. Quit Registry Editor, and then restart your computer.

For more information on this method go to:
http://support.microsoft.com/kb/896861/en-us