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'm working on a project which has localized site definitions. Now for one part, I wanted to use spmetal to generate a datacontext. Works, but I noticed the generated class retrieves lists by their name. I don't really understand why Microsoft would have picked this approach, since even a normal site admin can easily change the name of a list and thereby break the Linq class (or I'm missing something)?

I'd like to get the list by url and use that to query and insert items. Is this possible or do I have to revert back to CAML?

share|improve this question
add comment

2 Answers

Found a solution, but I'm not sure it's the best one. So if someone has anything better, please feel free.

Instead of

var test = context.ListName.Where(l => l.Title.Contains("test")).SingleOrDefault()

First get the SPList and then perform:

var test = context.GetList<Item>(list.Title)
     .Where(l => l.Title.Contains("test")).SingleOrDefault()

By using the correct (localized) title it should work.

SharePoint: SharePoint to Linq pitfall

share|improve this answer
    
+1 it's solution. It is 'ugly' one but as far as I know - it is the only one. Personally I don't like fact that I need to get SPList object twice to preform some 'easy' task so I am still using CAML. Note: I formatted your answer to make it more readable. –  Vedran Rasol Jan 20 '12 at 0:44
add comment

I localize all my solution (so my feature receivers etc. are completely generic) and agree you should never query by some hardcode value. But why do a Contains? I use a ResourceHelper to get the list title directly from the resource files based on the used SPWeb's Locale.LCID. It's a wrapper around SPUtility.GetLocalizedString.

share|improve this answer
    
As I see it he is using Contains to get all list items with "test" in Title field. Problem is how to get list name if list Display name is localized and you are usint Linq so he is getting list by url and GetList. Can you look at my answer here sharepoint.stackexchange.com/questions/7597/… and see if you can improve it? Also providing some basic code example of your method would be nice. –  Vedran Rasol Jan 20 '12 at 1:45
    
See my answer in the question you linked to :-D! –  Colin Jan 21 '12 at 0:42
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.