c#
27 08 2013
Trying never to use SPList.Items (due to Best Practice), yet needing to grab some items from a list quite often? Here’s an extension method which I use a fair bit.
|
/// <summary> /// Gets items where a property matches a value /// </summary> /// <param name="list">The list.</param> /// <param name="property">The property.</param> /// <param name="value">The value.</param> /// <returns>A list collection of SPListItems</returns> public static IList<SPListItem> ItemsWhere(this SPList list, string property, string value) { SPQuery query = new SPQuery(); query.Query = string.Format("<Where><Eq><FieldRef Name='{0}' /><Value Type='Text'>{1}</Value></Eq></Where>", property, value); SPListItemCollection items = list.GetItems(query); return items.Cast<SPListItem>().ToList(); } |
You can then use regular LINQ queries to get the item or items you need against the object returned by this, for example:
|
SPList webPartCatalog = site.GetCatalog(SPListTemplateType.WebPartCatalog); SPListItem wpItem = webPartCatalog.ItemsWhere("FileLeafRef", "Some Web Part.dwp").FirstOrDefault(); |
[…]
25 04 2013
(Concepts here are discussed around SharePoint 2010 but should equally be applicable to SharePoint 2013. All concepts here apply only to full-trust farm solutions, and cannot be done at all with Sandboxed solutions or with the App model)Occasionally you might give given the requirement (as a developer) to provide the ability to insert a chart […]
22 04 2013
Here’s some code to get all PerformancePoint filter selections for the current user in a web site. This is a fairly straightforward operation. First you need a collection of strings, each of which is the URL to a PerformancePoint Filter stored in SharePoint. In this code snippet I use SPSiteDataQuery (in the DiscoverFilterUrls() method). You […]
19 04 2013
Here’s a code sample which will give you a web part that will have a property (accessible via the Toolpane) that lets you define a semi-colon separated list of the names of Filter Connection points. You can then use any standard SharePoint (or even PerformancePoint) filters connected to the web part and use them how […]
14 02 2013
If, like me, if you’ve gotten into habit of wrapping most things that implement IDisposable in a ‘using’ statement, then you’ll be wondering why the hell you can’t read from the Requests’s InputStream property using a StreamReader without your request pipeline breaking. Turns out, that you mustn’t dispose of the StreamReader immediately, as when you […]
10 02 2012
I thought I’d jot down a few things which I believe fresh SharePoint developers need to know when they’re confronted with the daunting task of developing something for SharePoint for the first time. SharePoint development has a massively steep learning curve. It literally takes years to get good at not only developing solutions in SharePoint, […]
8 07 2011
As part of my series of blog posts on item level permissions, I thought I’d post a quick note on “resetting” item-level permissions on a list. This can be done using the method SPListItem.ResetRoleInheritance() on the list item in question. This will reset any item level permissions on this item to inherit from the List […]
1 06 2011
After this question was asked on SharePoint Stack Exchange, I thought I’d share my method of re-ordering content types in a list. Note: this isn’t the only method of doing this, see the thread on SPSE to see more (particularly the UniqueContentTypeOrder property).
|
private void ReorderContentTypes(SPWeb web, string listName, string firstContentTypeName)<br>{<br> SPList list = web.Lists[listName];<br><br> SPContentType cType = web.AvailableContentTypes[firstContentTypeName];<br><br> List<SPContentType> oldCTypes = new List<SPContentType>();<br><br> for (int i = list.ContentTypes.Count -1; i >= 0; i--)<br> {<br> if (!list.ContentTypes[i].Id.IsChildOf(cType.Id))<br> {<br> oldCTypes.Add(list.ContentTypes[i]);<br><br> list.ContentTypes[i].Delete();<br> }<br> }<br><br> foreach (SPContentType c in oldCTypes)<br> {<br> list.ContentTypes.Add(c);<br> }<br><br> list.Update();<br>}<br> |
18 04 2011
So you’ve embarked on writing a bit of code that must enumerate a SharePoint List’s Items one by one and do some stuff with them. You’ve (at some point) came across the Best Practices page on MSDN on Handling Large Folders and Lists, and seen that using SPList.Items is bad. It’s bad because you could […]
4 04 2011
Here’s a straight forward one. For some reason (don’t ask) I had to update a content type in a Feature Receiver (I would have preferred to have created a new content type and inherit) by adding a new field to it. Here’s the code, purely for my personal reference but open in case other people […]