Hot answers tagged

11

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\"/>" + ...


10

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


6

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


6

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


6

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

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

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

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


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

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

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

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


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

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

CAML deals with only those items which matches the filter criteria and they will retrived from database. Where Linq has the entire collection to manage with. Linq will Translate the query to CAML and send it to database and then fetch results. Hence CAML does not require the extra step.


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();


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 could try using the ViewCollection.GetByTitle or List.GetView method to fetch the already existing view. If the view is present, it will be returned and you wont need to create the new view.


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

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


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

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

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



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