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

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

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

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

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

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

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

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

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 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

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 ...


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

linq to sharepoint -in my opinion- is great, but yes it does have some limitations. From a development point of view I try to use it as much as possible knowing it's fairly new and will get even better. This article may help you decide whether to use it in your situation. http://www.sharepointblues.com/2010/09/08/linq-to-sharepoint-performance-pitfalls/ ...


2

you can use this, its part of that sharepoint object model SPList.ItemCount: private static void OpenListItem(string siteURL, string listName) { using (SPSite site = new SPSite(siteURL)) { using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists[listName]; ...


2

If you use LINQ, the code retrieves all records for a list from the content data base, in the case of CAML, CAML is performed to SQL query and retrieve data that match the criteria. For big data the CAML is the best choice. if you do not familiar with CAML you can use Camlex.NET. It helps to build queries by using expression trees.


2

Definitely CAML is the fastest. If you use LINQ using your code above it will fetch all list items into memory first and then apply the linq filtering. If you have thousands of items then thats bad news... Also, LINQ-to-Sharepoint is NOT normal linq like the code you've posted above. Linq-to-sharepoint maps content types to strongly-typed objects which you ...


2

Short answer is "Unfortunately, yes, you have to generete .cs files again". After changing the list structure you need to regenerate cs files or create custom mapping. Piece of code from Joerg Krause "SharePoint 2010 as a Developer Platform" (by the way, it is a very nice book and I advice you to buy and read it if you haven't read it yet): public ...


2

Use CAML, it will give you better performance. You should skip the <Query> And </Query> From your SPQuery.Query, so just start with <OrderBy> So query.Query = "<OrderBy><FieldRef Name='Sequence Number' /></OrderBy>"


2

Your code will instantiate every SPSite in the web application which will cause two problems: It'll be very slow if you have many Site collections in the Web Application It'll leak memory unless you dispose all of the SPSites. This implementation will be a lot faster and won't leak: public string GetSiteUrl(SPWebApplication webapp, string FullUrl) { ...



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