Wednesday 3 November 2010

Reverse Engineer Lists, Content Types and Site Columns and retrieving the CAML

Problem: You need to create deployable lists, this should be done using a feature.  You can create a lists using the UI and then get the CAML that created the list.  The code will have some hicups but it a good place to start for building your Lists based on List definitions, content types and their corresponding site columns.

Resolution:
Get the CAML and create your feature based on the declaritive CAML
http://%7bwebapplication%7d/%7Bsite%7D/_vti_bin/owssvr.dll?Cmd=ExportList&List={ListGuid}
Thanks to Sahil Malik

Read More:
http://blah.winsmarts.com/2008-2-Dev_Tip__The_SharePoint_University_of_Reverse_Engineering.aspx
http://www.synergyonline.com/blog/blog-moss/Lists/Posts/Post.aspx?ID=24

Download: SharePoint 2010 sandbox project example that creates 2 lists with a lookup field. Updated 24/03/2011


Monday 1 November 2010

SharePoint DateTime format is incorrect

Problem: The SharePoint DateTimeControl displays the date in US format i.e. 1033 or mm/dd/yyyy despite me changing the site collections regional settings.
Site Settings > Regional Settings > Locale > Select [English (United Kingdom)]

Inital Hypothesis: In MOSS changing the Locale did not change the format of the DateTimeControl. 
Resolution: This is still the case, you need to explicitly set the local on eash DateTimeControl.  You can set it to be the same as the site's local.
E.g. myDateTimeControl.LocaleId = SPContext.Current.Web.Locale.LCID 
Or set the "LocaleId"attribute of the SharePoint DateTime control
E.g. SharePoint:DateTimeControl runat="server" LocaleId="2057"

=========================
Tip: Changing the Regional settings will affect the curreny columns.

List of Locale's: http://msdn.microsoft.com/en-us/goglobal/bb964664.aspx

====================

Using CAML and SPQuery to query using dates

Problem: Using SPQuery I can't filter using a datatime parameter.
Hypothesis: I can see my custom web part code is formatting the DateTime selection (SharePoint:DateTimeControl) differently in my custom code CAML than the CAML generated by U2U.
Resolution:
Use the Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime() method to get your DateTime control value into the correct format.
SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.UtcNow)

More Info:
http://blogs.msdn.com/b/saurabhkv/archive/2008/05/05/spquery-with-boolean-and-datetime.aspx
http://snahta.blogspot.com/2009/07/spquery-few-important-things.html
http://www.aidangarnish.net/post/Using-SPQuery-and-CAML-to-filter-and-order.aspx

Friday 29 October 2010

SPGridView explained

Overview:  The SPGridView is similar to the asp GridView.  The SPGridView is derived from the aspGridView and has additional functionality.  The SPGridView does not support the property AutoGenerateColumns, you must specify each SPBoundField.

Example: SPBoundFields such as "BoundField" are used to display datasource columns.
 
Example: To format a bound column use the property "DataFormatString", e.g. colDate.DateFormatString = "{0:d}";, this will display the short date only from a DateTime type.  If you need more advanced formating such as for a multi choice field, create a new class that derives from BoundField and use it in the SPGridView.

Tip: Don't use DataKeyName on the SPDataGrid, it causes the error "Unable to cast object of type 'System.Int32' to type 'System.String'.".
Read More:
http://msdn.microsoft.com/en-us/library/bb466219(office.12).aspx

Multiple Choice Field

Multiple Choice Field

Field Class: SPFieldMultiChoice
Field Value Class: SPFieldMultiChoiceValue
Populating Information:

SPFieldMultiChoiceValue itemVal = new SPFieldMultiChoiceValue();
itemVal.Add("Choice a");
itemVal.Add("Choice b");
itemVal.Add("Choice c");
item["FieldName"] = itemVal;
item.Update();

Delimiter, you may want to delimite the multichoice field, this is ";#", or SPFieldMultiChoiceValue.Delimiter.  The delimiter can be useful for building SPFieldMultiChoiceValue objects or display purposes.

More Info:
http://sharepointcodeblock.blogspot.com/2008/07/properly-populating-and-retrieving.html
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldmultichoicevalue.aspx

SPDataGrid error 'Unable to cast object of type int32 - SPDataGrid.NewPageIndex property and DataKeyNames

Problem: SPDataGrid can't use DataKeyNames, it results in the error "Unable to cast object of type 'System.Int32' to type 'System.String'." when paging or filtering.


Hypothesis: This looks like a bug in the implementation of the SPGridView.  The property DataKeyNames causes the error.

Resolution: To pass values using buttons or check boxes inside you SPGridView rather use the row index to get the row and read the Id or data you need from the row.

More info:
http://www.sharepointoverflow.com/questions/4481/using-code-compiled-for-moss-2007-in-2010

Wednesday 27 October 2010

Programtically send email in SharePoint 2010

using Microsoft.SharePoint.Utilities;

if (!SPUtility.SendEmail(SPContext.Current.Web, false, false, "paul@demo.com", "subject", "Please read!"))
  {
    //TODO! log mail issue
  }