Archive for June, 2011

SharePoint 2010: Service Pack 1 is available for download

Leave a Comment

SharePoint Object Model: SPField – Internal Name vs Display Name

Scenario:

Some days back “We need to populate an asp DropDownList control with all possible values of a choice column created in a specific SharePoint list“. All done but when we tried to populate dropdowns using oList.Fields, following exception generated:

Field with name “XYZ” was not found.

Resolution:

We were using the following code:

SPField oField = oList.Fields[“XYZ”];

We were using internal name to get the reference of SPField and it was generating exception. The reason is that oList.Fields[] take following three values:

oList.Field[Guid id] Gets the field using unique identifier
oList.Field[int iIndex] Gets the field object at the specified index in the collection
oList.Field[string displayName] Gets the field with the specified display name for the collection

The trick is in the last one. We are used of getting list item value using internal name but here we are required to use display name. So, we switched our implementation to GUIDs as fortunately we are also creating list and fields using feature in the same project.

This also clears another concern that why we are required to use internal names instead of display name when accessing a list item specific field value using internal name like listItem["Field Internal Name"]. It may be due to the fact that user can any time change the display name of the field and our code will crash. As in the above example we preferred to switch to GUIDs instead to display name. Other than this, it may also be due to that fact that there are some characters which needs to be changed when translating display name to a URL.

Leave a Comment

SharePoint Live Chat with MVPs: Wednesday June 22nd

Microsoft schedule a chat session with the MVP Experts for SharePoint on: Wednesday June 22nd at 9am PDT

For more information:

http://msdn.microsoft.com/en-us/events/aa497438.aspx

Leave a Comment

SharePoint 2010: Display titles instead of URL’s in WhatsPopularWebPart

Scenario:

We need to display / render titles instead of the URL’s in “WhatsPopularWebPart”.

Resolution:

Google the problem and found following blog with an excellent idea how to accomplish above:

http://rolandoldengarm.wordpress.com/2011/04/08/whatspopularwebpart-render-titles-instead-of-urls/

As per “Roland Oldengarm” already stated that we “to created a webpart derived from WhatsPopularWebPart and implemented also ICallbackEventHandler”. Started working on it and found some implementation issues. First, we need to change the Regex and second, we need to change the code.


public class TestWebPart :

Microsoft.Office.Server.WebAnalytics.Reporting.WhatsPopularWebPart, ICallbackEventHandler

{

private const string ItemRegEx = @”<span dir=’ltr’>(?<url>[^<]*)</span>”;

private static string ReplaceUrlsWithTitles(string html)

{

if (Regex.IsMatch(html, ItemRegEx))

{

html = Regex.Replace(html, ItemRegEx, delegate(Match match)

{

var url = match.Groups[“url”].Value;

string title = url;

using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))

{

using (SPWeb spWeb = spSite.OpenWeb(title))

{

try

{

var item = spWeb.GetListItem(title);

if (item != null)

{

title = item.Title;

}

}

catch (Exception e) { }

}

}

return
“<span>” + title + “</span>”;

},RegexOptions.IgnoreCase);

}

return html;

}

string ICallbackEventHandler.GetCallbackResult()

{

return ReplaceUrlsWithTitles(base.GetCallbackResult());

}

void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)

{

base.RaiseCallbackEvent(eventArgument);

}

}

Comments (33)

Microsoft Office 365 Overview for IT Pros

Leave a Comment

PowerShell Script: Enable and Add Content Type in Document Library

Scenario:

We created content types using power shell script. Now, we need to enable content types and add these content types in the list.

Resolution:

$site = Get-SPSite “url of site collection”

$rootWeb $site.RootWeb

$docLib $rootWeb.Lists[“Shared Documents”]

$customContentTypes “ContentType1,ContentType2,ContentType3″

$docLib.ContentTypesEnabled $true

$docLib.update()

$customContentTypeArray $customContentTypes.Split(“,”)

foreach($customContentType in $customContentTypeArray)

{

    $customCT $rootWeb.ContentTypes[$customContentType]

    $docLib.ContentTypes.Add($customCT)

}

$docLib.update()

Comments (2)

Exam 70-573: SharePoint 2010 Application Development

Leave a Comment

Follow

Get every new post delivered to your Inbox.