Saturday 26 March 2011

Cannot delete related SP2010 lists

Problem: You cannot delete either of 2 lists if they are related through a lookup column.  I tried deleting the list instances using Powershell and the SP2010 UI.  The List definition and corresponding content types are being deployed and retracted successfully.  PS error message when deleting either list instance is:
Exception calling "Delete" with "0" argument(s): "This list cannot be deleted because one or more lists are related to it. A relationship between two lists occurs when one of the lists contains a lookup column enforcing a relationship behavior on the other list."
 
Initial Hypothesis:  As I am deploying my lists using content types and there is a lookup column, it is important that they are created in the appropriate order.  As I am chaning my features elements manifest file building out my lists it appears to be that I have got my lists instances tangled so they can't be deleted. 

Resolution: I deployed my solution so that the CT and list definitions were present, I deleted the lookup column using the list setting in the UI.  I could then delete the list instances. 

Friday 25 March 2011

SPMetal - Specified cast is not valid

Problem:  I am using LINQ to SharePoint to retrieve data.  A specific SPMetal list request generates the error "Specified cast is not valid".  Additional error information shown in the UI:
Web Part Error: Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred.
From the stack trace:
"at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at Microsoft.SharePoint.UserCode.SPUserCodeWorkerProcess.ExecuteDelegate.EndInvoke(IAsyncResult result)"
Hypothesis: The error message is a standard error thrown in a SharePoint sandbox solution. The underlying issue is that the proxy is working on lists using SPMetal however only 1 list is causing the problem.  I remembered that I had altered the list using Powershell.   Obviously the entity proxy class generated is based on the old list.  Generate the proxy with the latest lists.
Resoultion: Run SPMetal again to correct the proxy code as the proxy classes do not match the backend SharePoint lists. 

Tuesday 22 March 2011

SharePoint 2010 Search

SharePoint 2010 Edition comparison

Problem: Setup search securely from scratch
Initial Hypothesis: The setup wizard is not precise enough so it is a better idea to setup search using Powershell or even by creating a new Search Service Application using the UI.  The crawl account used by your Search Service Application can be seen under the "Default content access account" property.
When you create the new Search Service application you loose your existing search data such as indexing, scopes and search settings.
Resolution:
Corey Roth has a good post on permissions for the crawl account.

Security Trimming: Search only returns the results the current user has access to in SharePoint.  File servers work the same way.  Any outside content source you crawl using a specified account will not have security permissions and therefore will show all results.

Tuesday 15 March 2011

Presenting in Johannesburg on 4 April 2011

I'm presenting on Monday the 4th of  April at Corlett Drive for SharePoint 2010 Johannesburg Knowledge Group at 18:00 in the evening so if you are in the area, it will be good to see you. 

Event book and details: http://sp2010jbg.eventbrite.com/
Session summarys
Session 1
Presentation - Overview of data access in SharePoint. What are your options? Is SharePoint storage always the answer? What is LINQ to SharePoint? What is LINQ to SharePoint not good at? The 8 caveats to LINQ to SharePoint.
Session 2
Demonstrate LINQ to SharePoint 2010. Using Visual Studio creating a visual web part to perform CRUD operations on SharePoint lists.

Reference Material:
Project to create Lookup lists

Thursday 10 March 2011

Unit Testing for SharePoint 2010 - Part 2

Part 1 of Unit Testing for SharePoint 2010

Overview:  Part 1 discusses setting up nUnit and TDD.NET on you dev machine and writing a simple unit test.  This post looks at using TypeMock Isolator to unit test SharePoint dependent code.
//Arrange
//Act
//Assert
Background:  I haven't used TypeMock Isolator in the last 2 years but the latest version still is a good candidate for testing SharePoint 2010 based applications.   The TypeMock Isolator allows you to fake calls to SharePoint so that you can run unit testing against the SharePoint object model with actually having SharePoint installed or going to a specific instance of SharePoint 2010.  The full version (i.e. not the SharePoint only version of TypeMoch Isolator) also allows you to do create isolation mocks of you code (see Rhino Mocks is a good Isolator Framework).  So pretty useful for testing SharePoint or Entity Framework.  You simply mock out the expected behaviour and don't need to test the Entity Framework by connecting to it.

Tip:  You can use this exact same technique to test MOSS.
Tip: TypeMock needs a separate build server licence from the developer licences.  You can use the full product that includes the ability to do SharePoint testing or just the SharePoint TypeMock isolator.  As of 6 March 2011 the SharePoint edition costs 249 euros per year.  Licencing is expensive especially for large dev teams with multiple build servers but the returns are good if your organisation buys into TDD.
Tip: The SharePoint edition is fine for testing SharePoint calls but you can't mock your own .NET methods so you can't simply use a factory pattern and then mock your entire .NET method call as was shown to me by Ketul Patel (@Pateketu).

Objectives: Create a unit test that uses TypeMock Isolator that mocks the call to SharePoint.
TypeMock to Isolate a method call:
Add 4 reference for nUnit and TypeMock frameworks.
Add a reference to the library you are testing in my case "MagicBall.dll".
Create a new unit test, I isolate a call to the method "GetSPQuestions" and return a list of strings (this has allowed me to override the normal call that would be made to SharePoint).
My business logic for the "Magic 8 Ball" is shown below.  The key take away is I fake the method call GetSPQuestions() using TypeMock so it doesn't try access SharePoint.  This allows me to test my business logic using SharePoint's object model.
TypeMock to Isolate a SharePoint call:
The previous technique works if the return data is not SharePoint objects, so TypeMock allows you to mock up SharePoint objects.  Whereas this example shows how to unit test by faking SharePoint objects.
  • As you can see the code fakes the SPSite Object, then we fake the "Contracts" SPList object (fakeContractList). 
  • At this point all properties inside the objects are null.  So our list would simple return a list with no objects.
  • The next step is to create 3 fake list items that are added to the "fakeContractList".
The code below shows the business logic/code of my "Magic 8 Ball":
The method QuestionSPList() has child methods that use SharePoint objects.  This example directly fakes the SharePoint object model to allow for unit testing.
Summary:  I prefer to build my calls to the SharePoint API or any external API's in a low level private service method.  Then all I need to do it stub out the entire method as shown in the 1st example.  Faking the SharePoint object model using TypeMock is a good option for testing SharePoint reliant code. 

More Info:
Paul Hunt has done 3 great blogs on Andrew Woodward's SPRetreat so this will give you another viewpoint on unit testing for SharePoint 2010.

Andrew Woodward has a codeplex project (SharePoint Magic 8 Ball) that will give you tons of example code on unit testing.


Wednesday 9 March 2011

Problem with DIP using Lookup Columns

Problem: The Document Information Panel (DIP) in MS Office Word 2010 won’t do lookups to a list within MOSS.  This is a known bug. 
What is affected:  I have tested this on SP2010 and the issue has been resolved.  This only affects MOSS when using the Document Information Panel (DIP) for Word 2010, Word 2007 and Word 2003.

Initial Hypothesis: The issue is caused by MOSS User Interface (UI) when the lookup column is created. The UI links the document library content type to a custom SharePoint 2007 list using a GUID. The issue is that the GUID should be surrounded by {}. SharePoint UI knows how to handle these missing curly brackets however, all the MS Office applications (Word 2010 in our case) need the correct hook-up.  The hook up is wrong, hookup the document libary using a differnt approach.

Resolution:  Login to a WFE that has SharePoint Manager 2007 (codeplex project) installed. Navigate to the SavillsSolar Site Collection and amend the lookup property by surrounding the GUID with curly brackets. The url below details the change and additional information. 
Alternative Resolution: Create the custom list in the root of the site collection, the custom content type for the document library should have a lookup added.  This fixes the problem but requires content type to be create 1st.  Lookup list site column to be created 2nd.  3rd use the custom document contnet type in the library.  Lastly, the custom list has to be in the base/root of the site collection.
Alternative Resolution: Move to SP2010 not to practical but another reason to promote the move to your business.
Summary:  This is a rehash of Bernado Nguyen-Hoan's Blog post on the same topic that allowed me to quickly fix the issue on an old MOSS farm.  The new bit is that the lookup columns from SP2010 using the DIP have been fixed and the best appoach is to have your custom list (lookup list) directly under the site collection.

Source:
http://bernado-nguyen-hoan.blogspot.com/2010/01/problem-with-document-information-panel.html

Sunday 6 March 2011

Unit Testing for SharePoint - Part 1

Overview:  I am not an expert in Test Driven Development (TDD) but the SharePoint Retreat in London has made me think about using this approach for SharePoint 2010 development.  There are various tools to help you perform TDD however, these 2 posts show the approach I am most comfortable with thanks to Andrew Woodward. 

This whole article is based on Andrew's session and I really would recommend to any serious development team to get Andrew in to do some upfront consulting and potentially training.  Upfront costs and his experience would definately be quickly recovered.  If you have already decided to use a TDD get Andrew in to get you dev team up to a good standard with TDD for SharePoint, anyway enough of my sycophantic
dialogue.

Tools:
  • Visual Studio (VS) 2010 any edition;
  • NUnit - preferable to MSTest for SharePoint dev
  • TestDriven.Net (optional)
  • ReSharper (optional)
Objectives:
Part 1 focuses on using setting up a basic "Magic 8 Ball" to unit test the business logic using nUnit. 

Tip: using coding Katas (really good approach to training, this is the 1st time I've used them and they actually really work) to get a full unit testing system working. 
Tip: Write the unit test and then write the code to fix the test.

At the SharePoint retreat, each session started with a:
  1. Review of the last sessions code (if appicable),
  2. Followed by an overview of what will be the next bit of coding logic (instructor lead teaching/demo exercise),
  3. Break into a new pair programming team to code (old code is throw away and a fresh set of code is started that includes all the exercise from all the previous sessions), and
  4. Loop - Back to step 1 (Review each pairs code).
In this post I am describing the 1st session, what you are trying to do is setup unit testing and write a couple of tests for you "Magic 8 Ball". 

Setup
1. Open VS2010, create a new solution file (MagicPost1.Sln).
2. Create 2 new C# class projects, the 1st project contains the business logic code (BOL project) and the 2nd project contains the unit tests (BOLTest project).
3. Change the default class on the BOL project to Ball and the default class on the BOLTest project to BallTest.
4. Add 3 references to the BolTest project. 
All 4 steps are shown below:

dll reference locations:
\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\lib\nunit.core.dll
\Program Files (x86)\NUnit 2.5.9\bin\net-2.0\framework\nunit.framework.dll

5.  Add the basic testing structure to you BallTest.cs code as shown below:
6. Add this basic code snippet to your Visual Studio code snippets (Resharper has better code snippet support).  7. Write you 1st unit test (the simpler the better) as show below:
8. Build your project to ensure it compiles. 9. Comptete the test code as shown below and run the test.
Tip: Strictly following TDD principals your test should fail and then you write code to fix it in the BOL code.

More Info:
Andrew Woodward's codeplex project on the magic 8 Ball which provides the code you can use to build the magic 8 Ball with TDD.
Here is a slide deck to a presentation on the Magic 8 Ball TDD project.

Next Post:
Part 2 of the series looks at testing SharePoint using Type Moch Isolator.