Delta-N Blogs
VSTS Blog
Dev Blog
Sharepoint Blog
SharePoint Dev Blog
CRM Blog
Infra Blog
  

 

Go Search
Skip to main content

Categories
There are no items in this list.
Other Blogs
There are no items in this list.
Delta-N Blogs > SharePoint Development Blog
SPListItem value using the InternalName of a SPField
Field Displaynames can vary, for example because of localization.
It's safer to use the internal name. But a value can be fetched through the index or DisplayName (SPListItem) not through the InternalName (SPField).
 
listItem[index]
listItem["field displayname"]
 
So how to get the value from a SPListItem using the InternalName of a SPField?

SPField articleContentField = listItem.Fields.GetFieldByInternalName("PublishingPageContent");
if (articleContentField != null)
{
 articleContent = listItem[articleContentField.Title].ToString();
 //Title == InternalName
}
Misleading error messages

This error occurred when adding OWA (only!) webparts through the SP interface to a MySite. And it was caused by a disposal bug in a completely different webcontrol which has nothing to do with webparts.

So never use: using(SPContext.Current.Site)

Exception Details: Microsoft.SharePoint.WebPartPages.WebPartPageUserException: Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))

Source Error:
[No relevant source lines]
 

Source File: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\972613ff\2b03e207\App_Web_default.aspx_1261690241.r4iko1pw.0.cs    Line: 0

Stack Trace:
[WebPartPageUserException: Exception occurred. (Exception from HRESULT: 0x80020009 (DISP_E_EXCEPTION))]
   Microsoft.SharePoint.WebPartPages.SPWebPartManager.SaveChangesCore(SPLayoutProperties layoutProperties, Boolean httpGet, Boolean saveCompressed, Boolean skipRightsCheck, Guid& newTypeId, Byte[]& newAllUsersProperties, Byte[]& newPerUserProperties, String[]& newLinks) +2356

Add a Contact details webpart only once a page

It's not possible to add multiple Contact Details webparts (contactFieldCtrl) to one page, because it will result in a javascript error. This error is caused by a javascript function in init.js: MNUpdateImage
The function expects to find only one presence icon.

You can resolve the error by changing this line of code from:
var oldImg=obj.src;
to:
var oldImg=obj[0].src;
But only the most upper contact on the site will have the communicator menu available.
Then again it's not a good idea to change the system javascripts, so a better option is to consider generating the contact details by a content query or a custom webpart.

 

Adding views to a list programmatically
This example shows how to create and add a new view to a list. In this case it's a document library of MySite to which the view is added. The view displays all the Word documents in the document library.
 
 private void CustomizeDocLibViews(SPSite mysite)
{
   SPDocumentLibrary docs = GetDocLib(mysite);
   string[] docExtensionsWord = new string[] { ".doc", ".docx", ".dot", ".dotx" };
   AddDocumentLibraryExtensionView(docs, "Word Documents", docExtensionsWord);
}
 
private static SPDocumentLibrary GetDocLib(SPSite mysite)
{
   SPDocumentLibrary docs = null;
   try
   {
       docs = (SPDocumentLibrary)mysite.RootWeb.Lists["Personal Documents"];
  // SPList documentLibraryList = mysite.GetCatalog(SPListTemplateType.DocumentLibrary);
  // returns Personal Documents, but what about shared documents?
   }
   catch (ArgumentException argEx)
   {
       // list does not exist
   }
   return docs;
}
private void AddDocumentLibraryExtensionView(SPDocumentLibrary docs, string viewName, string[] extensions)
{
   try
   {
       if (docs != null)
       {
           SPView tmp = docs.Views[viewName];
       }
   }
   catch (Exception e)
   {
       // view does not exist
  //instantiate the viewcollection of the current documentlibrary
       SPViewCollection colViews = docs.Views;
  //Define query
       string query = "<OrderBy>";
       query += "<FieldRef Name=\"FileLeafRef\"/>";
       query += "</OrderBy>";
       query += "<Where>";
       query += "<Or>";
       foreach (string extension in extensions)
       {
           query += "<Contains><FieldRef Name=\"FileLeafRef\"/><Value Type=\"File\">" + extension + "</Value></Contains>";
       }
       query += "</Or>";
       query += "</Where>";
  //Define which columns should be shown on screen in the result
       StringCollection collViewFields = null;
       try
       {
    //columns of an existing view
           collViewFields = docs.Views["All Items"].ViewFields.ToStringCollection();
    //or define your own:
    //StringCollection collViewFields = new StringCollection();
    //collViewFields.Add("Column1");
    //collViewFields.Add("Column2");
       }
       catch
       {
           collViewFields = new StringCollection();
           collViewFields.Add("LinkFilename");
       }
  //Add the view
       colViews.Add(viewName, collViewFields, query, 100, true, false);
       // Add the view Based on an existing view             
       //collViews.Add(strViewName, oList.Views["All Items"].ViewFields.ToStringCollection(), strQuery, 100, true, false);
       //Add the view as a HTML View instead of a Grid View
       //collViews.Add(strViewName, collViewFields, strQuery, 100, true, false , Microsoft.SharePoint.SPViewCollection.SPViewType.Html, False)
   }
   catch
   { }
}
        
Format DateTime in a DateTimeField


It's not possible for a SharePointWebControls:DateTimeField control on a pagelayout to be reformatted. You have two options:

1. Add a code-behind file and a label to your pagelayout
 reformat the date in code-behind and display it in the label
 label.Text = Convert.ToDateTime(this.YourDateControl.ItemFieldValue).ToString("dd-MM-yyyy");

2. Add a new calculated field to the content type that you are using on the page layout with the following formula: = TEXT(DATEFIELD_ID,"dd-MM-yyyy")

Easily view all available properties for ItemStyle


Insert this template into ItemStyle.xsl to view all available properties and their values:


<xsl:template name="ShowXML" match="Row[@Style='ShowXML']" mode="itemstyle">
  <xsl:for-each select="@*">
   <b><xsl:value-of select="name()" /></b> [<xsl:value-of select="." />]<br />
  </xsl:for-each>
  <hr />
 </xsl:template>

Select ShowXML as the style in the content query webpart to view which properties can be styled using itemstyle.xsl

Using Dates in CAML queries.

Instead of formatting a date from a string, you can use the today keyword:

<WHERE>
<GE>
<FieldRef Name="StartDate"/>
<Value Type="DateTime"><Today OffsetDays=5 /></Value>
</GE>
</WHERE>

In case of more complex calculations on dates, you could make use of SPUtility.CreateISO8601DateTimeFromSystemDateTime in combination with the different calculation methods on the DateTime data type.
 
string datestring =
  Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime
  (DateTime.Today.AddDays(5));
string querystring = "<WHERE><GE><FieldRef Name=\"StartDate\"/>"
  + "<Value Type=\"DateTime\">" + datestring + "</Value></GE></WHERE>";
SPQuery query = new SPQuery();
query.Query = querystring;

http://www.u2u.info/Blogs/Patrick/Lists/Posts/Post.aspx?ID=1782

Date formatting in ItemStyle.xsl

When you use DateFormatting in the ItemStyle.xsl.
<xsl:variable name="Created">
<xsl:value-of select="ddwrt:FormatDateTime(string(@Created),  1033, 'MMMM dd, yyyy')" />
</xsl:variable>


Don't forget to add the namespace:
<xsl:stylesheet version="1.0" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">

Because it's not included by default.

 Tag Cloud

 Bloggers

Madeleine Use SHIFT+ENTER to open the menu (new window).
Yuri BurgerUse SHIFT+ENTER to open the menu (new window).

 ‭(Hidden)‬ Admin Links