Tag Info

Hot answers tagged

9

If you use linq to object, you always retrieve ALL records from DB. It is not good if you have got a lot of data. To reduce records you can use CAML query: SPQuery query = new SPQuery(); query.Query = "<Where>" + "<Eq>" + "<FieldRef Name=\"FileLeafRef\"/>" + ...


7

First of all you should just rule out the use of SQL. You should not use SQL directly against SharePoint content databases. If you ever do modifications directly using SQL that SharePoint Farm becomes unsupported for ever. Even just doing reads makes your SharePoint Farm unsupported as long as they are occuring and may cause any kind of problems as ...


6

I have finally solved the problem! I have tracked the LOG and found that line: Some lookup fields were omitted from the query results because the list exceeds the lookup column threshold. List URL: http://myserver/Listst/ListA ... To solve it, go to Central Administrator -> Application management -> Manage Web Applications select one, then on top ...


5

var items = from li in listSettings.Items.OfType<SPListItem>() where li["WebID"].ToString() == SPContext.Current.Web.ID.ToString() orderby li["Title"].ToString() select li;


5

Expression<Func<Task, bool>> get = t => t.Id == 2; string url = "http://localhost:12345"; string listName = "Tasks"; MySharePointSite1DataContext context = new MySharePointSite1DataContext(url); var list = context.GetList<Task>(listName); var task = list.Where(get).SingleOrDefault(); using GetList method of DataContext you can create ...


5

CAML is simply the fastest (if you write efficient CAML queries that is) for the obvious reason that there is no other query language for SharePoint. Linq to SP uses CAML internally The API methods does not convert to CAML and queries for the whole "collection" of objects The CAML query is converted to T-SQL internally by SP and queries the content ...


4

Use a pre-build event (not post-build!) as described in: http://msdn.microsoft.com/en-us/library/ee538587.aspx


4

Try using this extension method to get the actual string value of your enum. string theFullType = item.IDType.StringValueOf(); public static class Extensions { public static string StringValueOf(this Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); Microsoft.SharePoint.Linq.ChoiceAttribute[] attributes = ...


4

Chris O'Brien has a good blog post on the subject: Simple data access patterns for SharePoint Lists. You should also take a look at the guidance from the Microsoft patterns and practices group on the Repository Pattern.


4

Whenever you're using LINQ to SharePoint you should look at any CAML which is generated from queries (see How to: View CAML Generated by LINQ to SharePoint). Because if Linq2SP can't figure out how to generate a proper CAML it'll silently generate a limited CAML returning way more data than needed and not sorted and then do further selection and sorting in ...


4

For SPQuery, use SPQuery.Folder to define the folder: SPFolder folder = list.RootFolder.SubFolders["Folder 1"]; SPQuery query = new SPQuery(); query.Folder = folder; Then, to define content type, you should include the following condition into your Where clause: SPContentType contentType = list.ContentTypes["MyContentType"]; query.Query = ...


4

Yes, SPSite needs to be disposed But you can handle the disposal of SPSite through the enumerator using the extension method (see below): static class Extensions { public static IEnumerable<SPSite> AsSafelyDisposed(this SPSiteCollection sites) { foreach (SPSite site in sites) { try ...


3

Ok, I found the answer to my own question. It is possible with switching HttpContext objects. Here are the steps: Retrieve the user token from SPContext.Current Set HttpContext.Current to null Create a new SPSite with the user token Create a new Linq to SharePoint context This way the new context is created within the context of the prefererred user. I ...


3

As Waldek says in http://social.msdn.microsoft.com/Forums/en/sharepoint2010general/thread/df7b0a20-338c-41b4-bf35-d0cfefb39dc7 - only fields based SharePoint Foundation field types are used SPMetal. The publishing field types are Server specific. And the publishing fields are not intended for you to use directly, as you probably noticed they are not ...


3

I could be very wrong but from what I understand, SPMetal only generates code which replicates the structure of a list in the form of strongly typed objects - the generated code has no relevance on the contents of a list. (Please inform if incorrect). To only retrieve published documents you need to read up on LINQ to SharePoint, to find out how to filter a ...


3

Solution We build two methods. One to build Equal Expressions, and one to Contains Expressions // Listing 5 private static Expression<Func<TElement, bool>> BuildOrExpressionEqual<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values) { if (null == valueSelector) throw ...


3

var files = web.Lists["Allegati"].items .Cast<SPListItem>() .Where(item=>item.File!=null) .Select(item=>item.File) .ToList();


3

What you want to do is look into CAML field joins and projections As the name suggests, it allows you to join two lists linked by a lookup field, and bring up the values through a single CAML query. The page linked above is not a beacon of clarity but you can find good examples on Google. Using CAML will also enable you to put your Order by in it. A CAML ...


3

I got around this by extending the Item class in a new file because passing the <column name="" /> in the xml file and also trying <IncludeHiddenFields/> node did not work. This blog explains the process for extending the Item class. Here is an example of the code though you will need to add it to whatever namespace you are using for the rest of ...


2

If you want to view your CAML query without deploying and executing the code, you can use LINQPAD with the SharePoint 2010 Data Context Driver (from Codeplex). If you scroll down on the linked documentation page, you see that the CAML is generated under the SQL result tab.


2

Yes, as described in How to: View CAML Generated by LINQ to SharePoint, you just need to access the DataContext.Log property.


2

I don't have enough rep to comment, so I'll have to add this as an answer... Try creating a new view for the list [in sharepoint designer]. Add the columns you want to this view, and set it as the default view. Then, regenerate the SPMetal classes. Incidentally, if you're using SPMetal with linq in a publishing site with anonymous access, be prepared for ...


2

If you really have lookup field in your project, this is supported by Linq-to-SharePoint, but query logic is slightly different from SQL queries. But it is also not a rocket science, of course :) See the example below: var result = from book in dataContext.Books orderby book.Title select new { book.Title, book.Author.FirstName, ...


2

At the end of blog post you can find how to join and query two lists. Hope it'll help.


2

There is no need to convert the EntityLists to custom objects to be able to do joins, as long as you have lookups to join on since this is the key for doing joins in LINQ. In my demo fragments below I have few lists (Country, Supplier, Product, ProductCategory) with lookups between these lists. using (var context = new ...


2

If you're really sure you don't want to use SPWebConfigModification, you could look at SPWebService.ApplyApplicationContentToLocalServer() - this has the capability of merging entire sections into config files, but has the following caveats: Only applies to the local server (clue is in the name!) Must be called by a local administrator The combination of ...


2

You can create a protected method in the code behind that receives an object, then cast that to the proper type (in this case SPListItem) and from there extract the values and create the appropriate types (such as SPFieldUrlValue, ImageFieldValue, and so on). Same pattern can be easily followed to extract other kind of values from an object/item. Example: ...


2

OOTB Linq2SharePoint data context (generated by SPMetal) can be used in scope of web, not application. There is no easy way to expand the context to application scope.


2

I'm not sure if I understand you correctly, but if you have the list, you could get your item like this var myItem = myList.Items.Cast<SPListItem>().Where(n => n.File.Name == "x.txt").FirstOrDefault();


2

If the objective is just to query lists using LINQ and you can directly do that without SPMetal. LINQ can be directly used on SPListItemCollection e.g. SPlist CustomerList = Web.TryGetList("Customers"); var FindCustomer = from SPListItem Item in CustomersList.Items where Item["Orders"] as int == 5 select Item; //or ...



Only top voted, non community-wiki answers of a minimum length are eligible