Tech Space

The Technical Solution Space for SharePoint 2010, MOSS 2007, and .Net (C#, VB.Net)

SPFieldMultiLineText sampleField = sampleListItem.Fields["Sample"] as SPFieldMultiLineText;

string sampleText = sampleField.GetFieldValueAsText(sampleListItem["Sample"]);

//


// Returns the value of an Url-Field.
//


public static string GetFieldValueUrl(this SPListItem item, string fieldName)
{
if (item != null)
{
SPFieldUrlValue urlValue = new SPFieldUrlValue(item[fieldName] as string);
return urlValue.Url;
}
else
{
return string.Empty;
}
}

Example:

// Get the url of a SPFieldUrl field
string url = item.GetFieldValueUrl("URL");



//
// Sets the value of an URL-Field.
//


public static void SetFieldValueUrl(this SPListItem item, string fieldName, string url, string description)
{
if (item != null)
{
item[fieldName] = new SPFieldUrlValue()
{
Description = description,
Url = url
};
}
}


Example:

// Set the url and description of a SPFieldUrl field
item.SetFieldValueUrl("URL", "http://www.TestSite.com", "Test' Blog");

///


/// Set the values of a Lookup-Field with multiple values allowed.
///


public static void SetFieldValueLookup(this SPListItem item, string fieldName, IEnumerable lookupValues)
{
if (item != null)
{
SPFieldLookup field = item.Fields.GetField(fieldName) as SPFieldLookup;

SPFieldLookupValueCollection fieldValues = new SPFieldLookupValueCollection();

foreach (string lookupValue in lookupValues)
{
fieldValues.Add(GetLookupValue(item.Web, field, lookupValue));
}
item[fieldName] = fieldValues;
}
}


Example:

//Set the value of a SPFieldLookup field with multiple values allowed

string[] values = {"Hamburg", "London" };
item.SetFieldValueLookup("MultiLookupFieldName", values);

///


/// Sets the value of a Lookup-Field.
///


public static void SetFieldValueLookup(this SPListItem item, string fieldName, string lookupValue)
{
if (item != null)
{
SPFieldLookup field = item.Fields.GetField(fieldName) as SPFieldLookup;
item[fieldName] = GetLookupValue(item.Web, field, lookupValue);
}
else
{
item[fieldName] = null;
}
}


Example:

// Set the value of a SPFieldLookup field

item.SetFieldValueLookup("LookupFieldName", "LookupValue");

//


// Returns the value of a Lookup-Field with multiple values.
//


public static IEnumerable GetFieldValueLookupCollection(this SPListItem item, string fieldName)
{
List result = new List();
if (item != null)
{
SPFieldLookupValueCollection values = item[fieldName] as SPFieldLookupValueCollection;

foreach (SPFieldLookupValue value in values)
{
result.Add(value.LookupValue);
}
}
return result;
}


Example:
// Read lookup values of a SPFieldLookup field with multiple values allowed

IEnumerable values = item.GetFieldValueLookupCollection("MultiLookupFieldName");

//


// Returns the Single Value of a Lookup Field.
//


private static string GetFieldValueLookup(this SPListItem item, string fieldName)
{
if (item != null)
{
SPFieldLookupValue lookupValue = new SPFieldLookupValue(item[fieldName] as string);
return lookupValue.LookupValue;
}
else
{
return string.Empty;
}
}


Example:
// Reads the value of a SPFieldLookup field
string value = item.GetFieldValueLookup("LookupFieldName");

Recently discovered an issue in SharePoint 2007 when creating a new Page under Pages Library, we were getting "Access Denied" error message in "CreatePage.aspx" page. The users accessing the site have "Full Control" and they are getting the "Create Page" option in the "Site Actions" menu, so this should not be a security problem. This was really shocking and finally could find a solution for the same. The User or Group needs to also have "Read" permission to the master page gallery, and then this works fine.
Resolution:

  • Go to Site Actions -> Site Settings -> Modify All Site Settings.
  • Go to Galleries -> Master pages and page layouts.
  • Within the Master pages library/list, Select Settings -> Document Library Settings.
  • Select Permissions for this document library.
  • Add "Restricted Read" access to the User or Group.

After this we should not have any issue in creating Pages in pages library.