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
  }

Creating a lookup list & project field lookup using a declaritive content type

Problem: Create a list that has a lookup column and a projected field from the lookup column.
Example:
  • Some code I used to create a lookup column (Agency ID) in a list and a field to be brought in from the lookup list also (AgencyName)
  • This should be plugged into the new list's content type (AgencyContact).
  • The column being brought across also (Agency Name) needs to reference the GUID of the lookup column it relies on (Agency ID) using FieldRef.
Thanks to Paul W

Tuesday 26 October 2010

Updating the web.config

Problem: Updating the web.config by hand means you need to implement the change on each web servers web.config, so in a load balanced environment you need to do the change several times which leaves is open to mistakes and in the event of a rebuild or adding a new server, each web.config needs to be updated.

Hypothesis: You can either use a declarative approach or a programmatic approach.  Approaches on MSDN

Declarative approach is show on MSDN as the supplemental .config file You can implement the change using the ststadm cmd copyappbincontent or use a feature receiver
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var webApp = (SPWebApplication)properties.Feature.Parent;
webApp.WebService.ApplyApplicationContentToLocalServer();
}
Note: The feature must be scoped at "Farm" or "WebApplication" level otherwise you will get a Security exception, which makes sense.  RunWithElavatedPrivileges won't stop the error.

Resolution:  The declarative approach adds whatever is in the supplemenal config to the web.config, it does not check if it's already there so it is an option for a 1 off hidden feature however a better approach is to use an xml file that has a feature receiver that activates and deactivates the changes for maximum control.

More Info:
Additional information on using the declaritive approach.
Joel Jeffery's blog has a post on modifying your web.config programatically
Update 13 Dec 2010 - http://www.spdavid.com/category/SharePoint-2010.aspx

Monday 25 October 2010

Using SPGridView and Linq to display filterable lists

Problem: Display a SharePoint list and make it filterable. 

Initial Hypothesis: Create new page add the new Xslt List View Web Part (XLVWP) to the page. Customise the Xslt using SharePoint Designer (SPD).  The web part can now be exported if required.  Add filter web parts to filter the data for end users.

Resolution:  This gives a comprehensive solution for internal users but clients needing a specific design or advanced querying will need a different solution.  You can customise the xslt for the list and paging and sorting are built in.  However in my instance I needed drop down lists that could query against multi select choice columns. 

Problem Redefined: Using the SPGridView control display user's data based on selection criteria.

Hypothesis:
  1. Create a list that contains a choice column, the column should allow multiple choices.
  2. Ensure your generate a Linq to SharePoint proxy
  3. Create a Visual Web Part project that uses a visual web part to display the list results and the results are filtered by a drop down lists that works against the SharePoint list.
Resolution:
Filter the SPDataGrip using LINQ to SharePoint as shown below:
Step 1: Create the "Customers" list.
Stet 2 : Add the new column based on a multiple choice field type.

Shown above is my custom list "Customer", it has two columns namely: "Title" of type string and "Industry" of type choice (multi)
Step 3: Add a few random records.
Step 4: Create a new Visual Studio project, I chose a "Visual Web Part Project".
Step 5: Add the generate SPMetal code to your new project.  I used the SPMetal template provided by CKSDev but as long as you have added the SPMetal for the new list it will work.
Step 6:  Open the user control ascx and add a SPGridfiew and a drop down list.
Step 7:  In the code behind add the Linq query to retrieve the Customers list.
Different approach for using LINQ to SharePoint for this step is:

Step 8: Deploy you code and ensure it is working.