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