Wednesday, February 8, 2012

LINQ could not access SharePoint 2010 managed metadate list columns

LINQ is a very simple and powerful way of querying data in the .Net programming language as we mentioned before. It allows the .Net language to query data natively against strongly typed classes. Yes against class with objects through DataContext generated by SPMetal.exe tool. SharePoint will convert the LINQ to CAML during the runtime. As a result, LINQ is an easy way comparing to older CAML to work with list data. Here is example to use LINQ to SharePoint 2010 with tricks and limitations.

1. Create a list named “City” with three columns “City”, “State”, and MMD that is managed meta column on site http://sbx08/sites/Harry/.

2. Create several entries and file in MMD columns as in the screen shot


3. Use SPMetal.exe located in ..\14\BIN to generate DataContext so you could use the objects in LINQ
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN>spmetal /web:http://sbx08/sites/Harry/ /namespace:HarrySite /code:HarrySite.cs /language:csharp /useremoteapieapi

spmetal /web:http://sbx08/sites/Harry/ /namespace:HarrySite /code:HarrySite.cs /language:csharp /useremoteapieapi

4. Follow the procedure we mentioned before to add DataContext class. In the DataContext class HarrySite.cs, you will find the attributes “City” and State” for City list. However, there is no MMD attribute generated for this managed meta column. See the following  SPMetal generated code for City list.

[Microsoft.SharePoint.Linq.ContentTypeAttribute(Name="Item", Id="0x01", List="City")]
       public partial class CityItem : Item {
             
              private string _state;
             
              private string _city;
             
              #region Extensibility Method Definitions
              partial void OnLoaded();
              partial void OnValidate();
              partial void OnCreated();
              #endregion
             
              public CityItem() {
                     this.OnCreated();
              }
             
              [Microsoft.SharePoint.Linq.ColumnAttribute(Name="State", Storage="_state", FieldType="Text")]
              public string State {
                     get {
                           return this._state;
                     }
                     set {
                           if ((value != this._state)) {
                                  this.OnPropertyChanging("State", this._state);
                                  this._state = value;
                                  this.OnPropertyChanged("State");
                           }
                     }
              }
             
              [Microsoft.SharePoint.Linq.ColumnAttribute(Name="City", Storage="_city", FieldType="Text")]
              public string City {
                     get {
                           return this._city;
                     }
                     set {
                           if ((value != this._city)) {
                                  this.OnPropertyChanging("City", this._city);
                                  this._city = value;
                                  this.OnPropertyChanged("City");
                           }
                     }
              }
       }

If you try to use the column MMD from DataContext, it will NOT find the reference. This seems to be a bug and it is similar to the issue REST web service could not retrieve data form managed metadata reported before.

We will work with Microsoft and keep you posted.

No comments:

Post a Comment