Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a task to make a WSP, which should provide definitions of custom list and library, but not instances. List definition has a lookup column to library item

<Field ID="{F2572330-8163-460D-9016-0AF338B2AEF5}" Name="AssignmentDocumentsLocation" Type="Lookup" Mult="FALSE" Required="FALSE" DisplayName="Documents Location" Description="Root folder for documents of assignment" StaticName="AssignmentDocumentsLocation" Group="Assignments With Documents"/>

and it's set up at run-time, when list is created by code, something like

public class CreateAssignmentsDocumentsLibrary : SPListEventReceiver
{
   /// <summary>
   /// A list is being added.
   /// </summary>
   public override void ListAdded(SPListEventProperties properties)
   {
       base.ListAdded(properties);

       if (properties == null || properties.Web == null)
       {
           return;
       }

       var web = properties.Web;
       var list = properties.List;

       var listTemplate = web.ListTemplates["Assignments with documents"];
       if (list.TemplateFeatureId != listTemplate.FeatureId)
       {
           return;
       }

       var libraryTemplate = web.ListTemplates["Assignments documents"];

       var eventFiringEnabled = EventFiringEnabled;
       EventFiringEnabled = false;

       var libraryTitle = string.Format("{0} Documents", list.Title);
       var library = web.Lists.TryGetList(libraryTitle);

       if (library != null)
       {
            library.Delete();     
       }

       var libraryDescription = string.Format("Document library for storage of {0} corresponding documents", list.Title);
       var libraryId = web.Lists.Add(libraryTitle, libraryDescription, libraryTemplate);
       web.Update();
       library = web.Lists[libraryId];

       var assignmentDocumentsLocationField =
           list.Fields[new Guid("{F2572330-8163-460D-9016-0AF338B2AEF5}")] as SPFieldLookup;
       assignmentDocumentsLocationField.LookupList = library.ID.ToString();
       assignmentDocumentsLocationField.LookupField = "Title";
       assignmentDocumentsLocationField.Update();

       EventFiringEnabled = eventFiringEnabled;
   }
}

When I'm trying to create a LINQ to SharePoint models with different tools (manually calling SPMetal, create list instances and click context menu item "Generate entity classes" in "Server Explorer" window, or this extension to VS) I see that models have an association attributes for lookup fields with hardcoded lists names.

So my question is: is it possible to create LINQ to SharePoint model which should handle possibility of list renaming or even situation when one website contains two or more sets of my domain list and library?

share|improve this question
add comment

1 Answer 1

The important thing generated by SPMetal is all the Entity classes which has all to properties matching columns.

Another thing SPMetal created are properties in the DataContext like

public Microsoft.SharePoint.Linq.EntityList<Document> StyleLibrary {
  get {
        return this.GetList<Document>("Style Library");
      }
}

Which allows you to access the StyleLibrary using LINQ in an easy way as long as it's called "Style Library" like this:

var x = for x in context.StyleLibrary ....

But if the list can have another name you can just use

var x = for x in context.GetList<Document>(listname) ...

unfortunately LINQ2SP only allows you the get the list by name, so you have to have code which finds this from the URL or whatever.

share|improve this answer
    
I made a code to get list by id, but I would like to know how LINQ will manages mapping of lookup fields? If it has a hardcoded list name in "Association" attribute how to change it in run-time? –  Oleg Savelyev Aug 22 '12 at 9:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.