34

This is related how instance of SPListItem is being created. Actually in both cases new item is added to the collection of items in list, in first case new item is added explicitly, in the second one implicitly. The point is, that for initializing collection of items in list is used the same method GetItems but with different queries. SPList.Items.Add()...


28

It sure looks so from your story, but isn't there a quite easy workaround for this? <Where> <Or> <In> <FieldRef Name="ID" /> <Values> <Value Type="Number">1</Value> <Value Type="Number">2</Value> . . . <Value ...


11

Generally speaking you will want to use SPQuery to only query items you're interested in. Unless you're doing SPList oList = web.GetList("XYZ"); for(int i=0; i < oList.Items.Count; i++) { string strLstItemName = oList.Items[i].Name; // << BAD, as you use Items here, so you fetch them from DB each loop } there shouldn't be any relevant ...


6

You can use the SPWeb GetFile method to retrive the SPFile located at a specified url. Try something like this: SPFile file = SPContext.Current.Web.GetFile(string.Format("{0}/{1}", x, y));


6

Make sure that the column you include in the where clause is defined as an indexed column in SharePoint. You can check by going to the list you're querying and into List -> Settings -> Indexed Columns. It's not applicable in your CAML query, but if you have more than one 'where', ensuring the first where hits an indexed column can increase efficiency. ...


5

You can use: if (bool.Parse(item["ShowTitle"].ToString()) == true) or if (item["ShowTitle"].ToString() == true.ToString())


4

Working solution from StackEchange link var context = SP.ClientContext.get_current(); var myListItem = context.get_web().get_lists().getByTitle("ListName").getItemById(1); context.load(myListItem); context.executeQueryAsync(function(){ alert(myListItem.get_item("HyperLinkFieldName").get_description()); alert(myListItem.get_item("HyperLinkFieldName")....


4

Can't comment due to low rep so posting as an answer, purists forgive. Breaking a 500 element In into several nested Or tags does not work, to my despair. The limit is 500 elements hard apparently. I have tried breaking using a limit of 500 elements, 400, 300, 200, 100, and the SPServices query failed, returning: 500 Internal Server Error with the ...


4

You could sort list items using array.sort function: function getListItems(listTitle,success,error) { var ctx = SP.ClientContext.get_current(); var list = ctx.get_web().get_lists().getByTitle(listTitle); var items = list.getItems(SP.CamlQuery.createAllItemsQuery()); ctx.load(items); ctx.executeQueryAsync(function() { success(items); ...


3

You have to iterate through DataTable and fill into SPListitemCollection object. Also check following link for the code details: http://social.technet.microsoft.com/Forums/sharepoint/en-US/f772b759-28b7-4330-b4a2-ef7541f3e79c/how-to-convert-datatable-into-splistitemcollection


3

You can get items collection very simply. Try this code: public static SPListItemCollection GetItemsRecursive(SPFolder folder) { SPList list = folder.ParentWeb.Lists[folder.ParentListId]; SPQuery query = new SPQuery(); query.Folder = folder; //set folder for seaching; query.ViewAttributes = "Scope=\"Recursive\""; //set ...


3

This answer has two parts: First, for maximum performance and scalability, to retrieve the lists you should be using web.Lists[Guid]. Iterating through web.Lists and picking your selected lists, or using web.Lists[Name] (which also iterates through the set internally) will causes the API to retrieve metadata for all SPList objects under that SPWeb, which ...


3

There is no memory leak in your code for SPWeb. The SPWeb object you get from SPContext.Current.Site.OpenWeb(webUrl) should be disposed (like you already did). There is are other performance improvements I can suggest in your code: 1) Use list = web.GetList(lstUrl) instead of list = web.Lists[listName] ; 2) Use list.GetItems([SPQuery]) instead of list....


3

You're disposing an instance of SPWeb got through using SPContext. This probably will result in unpredictable behaviour as something else is expecting to dispose of the Site object you've got it from. Rather, use new SPSite(SPContext.Current.Site.Url) and wrap that in a using tag like you've done there. EDIT: Yeah, you're using OpenWeb() which does indeed ...


3

You can use the Order By in CAML: <OrderBy> <FieldRef Name = "ID" Ascending = "TRUE"/> </OrderBy> <Where> ... </Where>


2

I think this should work: SPFolder folder = myItem.File.ParentFolder; SPFileCollection items = folder.Files;


2

Blockquote I don't like using [0] to get the SPListItem...interested in reccomendations Well, SPList.Items[0] syntax fetch ALL THE DATA from SharePoint and after - give you item by index. That's a common behavior. You might use also SPList.GetItemById or similar methods or even make a caml query against the target list w/ SPList.GetItems method. It's up ...


2

There is no buildin way to get all the lists in a site collection. Your method of looping through AllWebs and getting the Lists collection for each is the only way, but it's not efficient, so you probably want to build some cache or maintain your own list and updated it using a EventReceiver each time a list is create/delete or when a site is deleted. But ...


2

All answers are not safe, when the field is null. This can happen, when a field gets later added and already items are in this list. This is a safe cast: bool? showTitle = properties.ListItem["ShowTitle"] as bool?; if (showTitle.HasValue && showTitle.Value) { }


2

I think this would be the cleanest way of doing it with C#: if ((bool)item["ShowTitle"] == true) { //Do stuff }


2

The value of an Url field is stored internally as an url/description couple. What you are fetching is the string representation of that value. To obtain the actual values, the preferred way is to use the SPFieldUrlValue class. You need to create a new istance of the class from the field string value - you will then be able to read the desired url and ...


2

A little more background, AddItem() is the preferred method because as the list gets larger, it has to download all the items in the list when you call list.items.add() in order to add the one item. This is why MS created the AddItem() call so that you could add an item without doing a query of all items in the list. Avoid calling .Items off of a SPList ...


2

Alternatively, you could use SPFieldLookupValue (for a single lookup) and SPFieldLookupValueCollection (for a multi-lookup) to parse the field values using SharePoint's built-in methods for processing them. Here is a revised version of your function for single lookups: protected string RemoveCharacters(string fieldValue) { SPFieldLookupValue value = ...


2

For this you need to increase the Threshold value first and then get the list item count. You can increase it either programmatically or powershell or Central admin. Ex.. through powershell $WebApplication = Get-SPWeb http:// $List = $WebApplication.Lists["My List"] $List.EnableThrottling = $false $List.Update() http://social.msdn.microsoft.com/Forums/...


2

The above mentioned techniques would have only work if DataTable would have been derived from SPListItemCollection. Since its not the case, so you will have to iterate over the rows in DataTable and will have to create list items for each row and associate it with appropriate properties.


2

There are two options in sharepoint to delete the listitem: listitem.Recycle() deletes the list item and puts it in the recycle bin listitem.Delete() deletes the item permanently If you delete a document in SharePoint using the User Interface, it’s being moved to the Recycle Bin, so that it can be restored if necessary. Calling the SPListItem.Delete() ...


2

In the context of general c# comparistions between for and foreach - There is a pretty comphrensive discussion here for loops on List are a bit more than 2 times cheaper than foreach loops on List. Looping on array is around 2 times cheaper than looping on List. As a consequence, looping on array using for is 5 times cheaper than looping on List ...


2

Author is the person who created the item. It is assigned once the item is created and never changed. Editor is changed every time item is modified. Better use SPFieldUserValue to retrieve SPUser. foreach (SPListItem item in myItemCol) { SPFieldUserValue usrAuthor = new SPFieldUserValue(SPContext.Current.Web, item["Author"].ToString()); author = ...


2

This post answered my question. In Summary using System.Linq; Then List<SPListItem> allItems = new List<SPListItem>(); allItems.AddRange(list.GetItems(spQuery).Cast<SPListItem>().ToList()); allItems.AddRange(list2.GetItems(spQuery).Cast<SPListItem>().ToList()); //Do something to the "allItems"


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