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.

Friday, 22 October 2010

Extranet user access options - Farm architecture design considerations

Problem: What is the best approach for an extranet?

Hypothesis:
You are implement SharePoint 2010 and you need users to have remote access, the 2 key technology impact areas are:
• Authentication to SharePoint 2010; and
• Remote access mechanism (External users).
I will deal with each question separately for simplicity.

Authentication
SharePoint 2010 has 2 mechanisms for authentication: classic (MOSS approach) and claims based. I would almost always recommend using Claims based authentication as this allows you to hook into any credential provider such as LDAP, AD, SQL or a custom provider. For example a recent project involved authentication from Active Directory & an IBM LDAP provider, this was relatively easy to do using claims based authentication.  If you have a custom Single Sign-On (SSO) solution you can write your own claims provider.
Remote Access
The answer for where to deploy SharePoint is … “It depends on what you have currently, your licencing and security requirements.”

Option 1
You can put SharePoint in your DMZ (public area) and use a reverse proxy and allow users access to you SharePoint farm. You will probably want to use SSL between the external end users and the reverse proxy.  The traffic between the SharePoint Farm the proxy won’t be under SSL. This approach is good for easy access, high performance and easy setup. This approach is not as secure as getting users to login to your network using VPN access. The advantage is you merely need SSL setup (my preference is firewall SSL termination i.e BlueCoat, most hardware firewalls offer this including F5’s Big-IP, you can also use the latest ISA/UMG proxy software to do SSL termination. Microsoft’s ISA 2006 has SSL termination built and is a pretty good solution. I believe UMG is the update to ISA.  SharePoint requires sticky sessions so if you are using a hardware or software based load balanced, you must have sticky sessions enabled.

Option 2
Another option is to use SSL NIC’s or my least favourite but most commonly used option is SSL on the server. You can still have your firewall in place but you don’t use a reverse proxy. I would definitely go for a reverse proxy with external access over SSL but this options is available on smaller SharePoint farms.  You can also load balance using NLB.  SSL adds about a 35% overhead to CPU resources on the server and SSL NIC cards can drastically improve CPU utilisation, however I would prefer to terminate at the firewall if possible.

Option 3
VPN solutions – Either IPSec or SSL-VPN’s are a good option but it depends on your infrastructure as setting up a VPN is a program in itself. Remote offices are already on VPN or some tunnelling technology but this doesn’t work for home users or 3rd parties as you need to give them remote access which generally involves RSA token or some sort of security token. This is the most secure option and once a user is on the network they will access your private SharePoint farm over http.

Resolution:  The general answer as always in SharePoint is "It depends ...".
I would recommend profiling all type of possible users something like

Role: Head Office User
Description: Http access from the LAN authenticating using AD.


Role: Satellite Office User
Description: Remote office has a VPN tunnel/MPLS to the head office, employees are authenticated using AD and will use http to access the SharePoint farm.

Role: Employee at home
Description: http (SSL-VPN) AD Employees have SSL-VPN access and once connected used http to view the SharePoint farm.  Authenticated using AD.


Role: 3rd Party users https SQL database or AD
Description: Suppliers need to with SharePoint, they will access 2 SharePoint load balanced servers in the DMZ using https.  The firewall is an ISA server 2006 which offers SSL termination and host header redirection.....

You can also surface the same site using different urls, so have an internal site and then a site in the DMZ.

When doing your farm architecture other points to bear in mind are redundancy, performance & licencing.  This coupled with your existing technology status and your SharePoint will provide a accessible, stable, scalable, preformant, secure SharePoint farm.   Designing your farm will reduce you licencing costs dramatically while providing the best solution to the business.

Thursday, 21 October 2010

Feature is always activated for features targeted at a web site (web)

Finding: Feature scoped at "web" is always activated when deploying using Visual Studio 2010.  Regardless of the "Activate on default" setting, - thanks Matthew.

Tuesday, 12 October 2010

CKSDev tool issue - The custom tool SPMetalGenerator failed

Problem: When adding a SPMetal Definition (CKSDev) item in visual studio, the SPMetal proxy generation fails with the error message: The custom tool 'SPMetalGenerator' failed.  Object reference not set to an instance of an object.

Hypothesis: CKSDev item template is failing, next I tried SPMetal from the command line and received the "Invalid File Name" error.  I had previously fixed this issue and here is the link.

I check the issue using the browser:

Resolution: Delete the list showing the Invalid file name error.  I could not do it from the UI so I used PowerShell.

More Info:
http://sp2010uk.blogspot.com/2010/08/issues-with-site-columns-content-types.html

NullReferenceExcepetion when viewing/editing an existing list item after the list instance has been deployed

Problem: When viewing or editing and item in a list, I receive an application error (NullReferenceException).  This happens when I redeploy a list, my list is defined using site columns, content type and the list definition is based on the content type.

My ULS logs contained the following information:
Failed to cache field with id
Unable to open Lookup list 'Lists/xxxList'.[Error was 0x8007....]
System.NullReferenceException: Object reference not set to an instance of an object.   at Microsoft.SharePoint.Publishing.FieldCache.TryGetValue

Initial Hypothesis:  ULS logs show me that a lookup list is not being found.  By examining the columns on the list instance I can see they are not being created correctly.  My initial thought was my xml was wrong, once it was reviewed and I found it to be correct I realised it was a caching issue.

Resolution:  Clear the Visual Studio Cache
  1. Right click on "Project" in Solution Explorer and click "Retract";
  2. Close Visual Studio 2010; and
  3. Open the "Project" in Visual Studio 2010 and "Deploy" the solution. 

Monday, 11 October 2010

SPMetal naming conventions - good practice

Problem: The Linq to SharePoint proxy generates properties using the display name of the column, additionally spaces are removed.  If you use a friendly short name you get duplicates.  The 2nd instance is appended a integer.  If you add a column with the internal name "Advert Title" and it's display name is "Title", SPMetal generates 2 properties namely: Title (the default column in all lists) and Title0, not ideal to differeintiate in code.

Resolution:  Make the display name more specific to avoid the scenario.  I.e. "Publication Title" would become the property "PublicationTitle".

Tip: Update 18 Oct 2010 - SPMetal does not like spaces in the url to the site that it generates off.  Error the web at 'http://demo.dev/sites/my site' could not be found

Friday, 8 October 2010

Extending SPMetal - Retrieving Attachments from SharePoint Lists

Problem: LINQ to SharePoint does not support attachments by default.
Hypothesis: Traditional Server Object Model approach to retrieving a specific list item’s attachments.  Approach is shown below:
Resolution: Use a partial class as described by Andrew Connell to use Linq to SharePoint to work with attachments. You could hydrate the attachment (SPFile objects) in the parital class but I choose not to, in case someone in the development team used the extended mapped property for retrieving a large list. This uses the Server object model and the hydration process is extremely heavy.  This method allows me to know if there are attachments, how many and where I can get them.  With this information I only get the specific SPFile objects if I need to work with the object.  Outlined are the steps to extend LINQ to SharePoint to retrieve attachment associated to items in a list.

1) Extend the SPMetal generated proxy class
2) Get a specific item using the LINQ to SharePoint model and using the String array returned by the extended class, get the file objects using SharePoint’s server side object model.
Tip: attachments is a string array, not an array of SPFile objects. The attachements.UrlPrefix property can be used to hydrate the SPFile objects for the list item.

Tip: I upload attachments using the Dialog framework point to attachfile.aspx as it does all the work for me.
_layouts/attachfile.aspx?ListId={5555D16F-5559-4CE4-555E-F5B0E9DC5555}&IsDlg=1&ItemId=



Thursday, 7 October 2010

SQL Server 2008 Service Pack 2 Released

Overview: Technet news flash email letter announced the release of SQL Server 2008 Service Pack 2 today.  I think it's been out a few days. 
http://go.microsoft.com/fwlink/?LinkId=196550
I have installed on my development machine - no issues.  If you are on Windows 2008 upgrade to SP2.  New installs should still use SQLServer 2008 R2 and ignore this Service Pack.

Wednesday, 6 October 2010

jQuery intellisense

Problem: jQuery is pretty useful and I'm not great at creating it.
Hypothesis: I am using jQuery version jquery-1.4.2.js to get intellisence I need to reference jquery-1.4.1-vsdoc.js, this should be removed at deployment.  You can reference the Visual Studio jQuery Intellesense file (jquery-1.4.1-vsdoc.js) either from microsoft's CDN or deploy locally to the 14 hive.

References:
http://weblogs.asp.net/scottgu/archive/2008/11/21/jquery-intellisense-in-vs-2008.aspx
http://www.devcurry.com/2010/02/visual-studio-jquery-intellisense-over.html

Samples:
Get the input from a multiline text box/Textarea
var existingComments = $("[id*='txtComments']").val();
Explaination - txt comments is an text box namely: asp:TextBox runat="server" ID="txtCommentsAmend" TextMode="MultiLine".  val() gets the inner html/content.
Append additional data to the text box
$("[id*='txtCommentsAmend']").html(existingComments + 'more info');
Explaination - using the previous example variable append additional text in the textbox.

Saturday, 2 October 2010

Powershell to Move a Site Collection to a new Content Database

Problem: I have several site collections inside a single content database.  The content database is growing rapidly.  Backup and restore is fairly slow and SQL can start slowing down if the db is large enough.

Hypothesis: Create at least additional content db, move specific site collections to the new Content database.  I was surprised that moving site collections to content databases is not in the Central Admin (CA) UI.

Resolution: Create an additional content database either using PowerShell (PS) or the UI namely:
1.> CA > Application Management > Manage Content Databases > Add a Content Database (I called my content database "WSS_Content_Empty").
2.> Move the Site collection to the new content database using PS
3.> Check the database has been moved in CA

More Info:
http://technet.microsoft.com/en-us/library/cc825328.aspx

Set New Content Database using CA Only:
  1. UI only option to achieve the same result would be to create the new content database using CA as shown above. 
  2. Next using CA go to "Manage Content Databases", select the old/existing content database and set it to offline (This means that you can't add new content databases, existing site collections are still available).
  3. Adding a new Site Collection will now use the new/next content database that is ready/online.   
End of Post

Backup and Restore Powershell commands:
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
cls
Backup-SPSite -Identity 'http://my.demo.dev/sites/kbtest' -Path C:\Projects\Backup\test.bak -Force -NoSiteLock -UseSqlSnapshot -Verbose
Restore-SPSite -Identity 'http://my.demo.dev/sites/newkbtest' -Path C:\Projects\Backup\test.bak -Force -Verbose 

Friday, 1 October 2010

Save site as template link missing in site settings

Problem: You want to create an WSP for your site however, the "Save site as template" link is missing from the "Site Settings" page as shown below.
Hypothesis: Deactivate the Publishing features namely: Manage Site features > Sharepoint Server Publishing & Site Collection features > SharePoint Server Publishing Infrastructure.   You know have the ability to create site templates i.e. wsp files.  The problem is you don't have your publishing infrastructure.

Resolution: If publishing is enabled on your site, craft a url so you can create the wsp file.  Url is http://demo/_layouts/savetmpl.aspx

SP2010 Connections Conference in The Hague - Day 2

Managed Meta data in custom solutions by AC was a real eye opener.  Managed Metadata Service (MMS) is very powerful and i need to be using it to a much greater degree.  AC's earlier talk on WCM was interesting but not a technical discussion but we were warned so not AC's fault. 

Mirjam van Olst gave a presentation on Logging, Monitoring & throttling.  It's the 1st time I've been to 1 of her presentations and I was hugely impressed she really knows her stuff.

Dan Holme gave a good session on Information Architecture (IA) and the MMS.  Useful as after AC's session i decided i definatelty needed more MMS info.

The last session I went to Wouter van Vugt's session on Access Services - Access services could potentially be huge in SP2010 and it was good to see a techy like Wouter highlight what it was good and bad at.

Generally a good day and a good conference.  Not exactly Vegas 2009 or even Berlin 2007 in quality but worth the trip and met some nice people that are really passionate about SharePoint.