Posts
Webparts on a layoutpage which is provisioned several times
It is pretty easy to add webparts to a layout page. Just define the webparts in the corresponding Elements.xml file. Like you do in onet.xml. It works fine if you provision just a single page with this layout on a web. If you have multiple pages that use the same page layout (e.g. in WCM). The trick is to use the attribute IgnoreIfAlreadyExists in the File tag:
IgnoreIfAlreadyExists="True" Comments from Wordpress.
Posts
TryGetList
How do we get a list? Perhaps like that:
var list = web.Lists\[listname"\]; ```But we must be aware of exceptions that can appear and we must handle them. A better way to get a list is to use [TryGetList](http://sharepoint.stackexchange.com/questions/18035/custom-webpart-being-rendered-twice-on-layout-page): var list = web.Lists.TryGetList(listname);
Posts
WithWeb-pattern of Jonas Nilsson
Jonas Nilsson shows an interesting approach for working with SPSite and SPWeb which must be disposed. Create a helper method WithWeb and send an Action parameter:
public void WithWeb(string uri, Action<SPWeb> action) { using (SPSite site = new SPSite(uri)) { using (SPWeb web = site.OpenWeb()) { action(web); } } } ```Here is my implementation of this pattern: public static class DisposalService { public static void WithWeb(string uri, Action action) { using (var site = new SPSite(uri)) { using (var web = site.
Posts
Custom Title on built-in webpart
What to do if you want to change the title in the built-in webpart like Tasks or so. Your own title or just to avoid tasks (1) and tasks (2) if you add two views of them to a page. In onet.xml you add them as view:
<View List="$Resources:core,lists\_Folder;/$Resources:core,tasks\_Folder;" BaseViewID="7" WebPartZoneID="Middle" WebPartOrder="6"/> ```The solution is to add [cdata element and webpart tag within it](http://www.novolocus.com/2011/06/14/set-the-title-of-a-listviewwebpart/). So now replace this with: <![CDATA[ Microsoft.
Posts
Compare two TimeSpan objects
var TimeToIgnore = new TimeSpan(0, 0, 0, 60); var created = (DateTime)properties.ListItem[SPBuiltInFieldId.Created]; var now = DateTime.Now; var span = now.Subtract(created); return span.CompareTo(this.TimeToIgnore) < 0;
Posts
Server Relative Url of an SPListItem
The easiest way to get the server relative url of an SPListItem is to retrieve the property “ServerUrl”.
SPListItem item... var url = item\["ServerUrl"\];
Posts
Check if user is in group
Use LINQ to check if user is in a group. Create an extension method.
public static bool InGroup(this SPUser user, SPGroup group) { return user.Groups.Cast<SPGroup>() .Any(g => g.ID == group.ID); } ```EDIT 2011-01-22: There is a shortcoming of this method. You won't get a user which is in group through a AD group. You'll get only users and ad groups. [But there is another method to check if a user is inside an AD group](/2012/01/16/check-if-a-user-is-in-a-ou/ "See my post about how to retrieve users from AD groups with PrincipalSearcher").
Posts
PublishingRollupImage
The internal name of Rollup Image or Upplyft bild is PublishingRollupImage
Posts
Content Type Id for Image, Audio and Video
After debugging I have found the Content Type Ids for Image, Audio and Video in the assets library. These content type ids are not present in SPBuiltInContentTypeId.
public class SPBuiltInContentTypeIdExtension { public static SPContentTypeId Video = new SPContentTypeId ("0x0101009148F5A04DDD49CBA7127AADA5FB792B00291D173ECE694D56B19D111489C4369D"); public static SPContentTypeId Audio = new SPContentTypeId ("0x0101009148F5A04DDD49CBA7127AADA5FB792B006973ACD696DC4858A76371B2FB2F439A"); public static SPContentTypeId Image = new SPContentTypeId ("0x0101009148F5A04DDD49CBA7127AADA5FB792B00AADE34325A8B49CDA8BB4DB53328F214"); } These three asset content types inherit from Document CT (“0x0101”) and have “0x0101009148F5A04DDD49CBA7127AADA5FB792B” in common, which is the content type id for multimedia content type.
Posts
Rensa text från html-taggar
Ett bra exempel finns här.
using System.Text.RegularExpressions; ... const string HTML\_TAG\_PATTERN = "<.\*?>"; static string StripHTML (string inputString) { return Regex.Replace(inputString, HTML\_TAG\_PATTERN, string.Empty); }