Sharepoint repos on github
You may think all interesting open source Sharepoint projects on codeplex. Well it is true, but there are some interesting projects on github as well. There are 17 projects for now. From a matrix to RichControls.
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.com
Webpart in a reusable user control « Sharepoint. Kunskap. Upptäckter på resan. - Sep 1, 2011
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);
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.OpenWeb()) { web.UnsafeUpdate(action); } } } public static void WithElevatedWeb(string uri, Action action) { SPSecurity.RunWithElevatedPrivileges(() => WithWeb(uri, action)); } }
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.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c Microsoft.SharePoint.WebPartPages.XsltListViewWebPart Task Title as I want it ]]>
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;
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"\];
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"). How could we combine them?... I think we must start from group this time, not from user:
public static bool HasUser(this SPGroup user, SPUser user) { var users = group.Users.Cast(); var samAccount = Regex.Replace(user.LoginName, @".*\\(.*)", “$1”, RegexOptions.None); var exists = users.Any(u => u.LoginName.Equals(user.LoginName)); if (!exists) { var ctx = new PrincipalContext(ContextType.Domain); foreach (var u in users) { var login = u.LoginName; var groupName = Regex.Replace(login, @".*\\(.*)", “$1”, RegexOptions.None); var grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName); if (grp == null) continue; var principals = grp.GetMembers(true); exists = principals.Any(p => p.SamAccountName.Equals(samAccount, StringComparison.InvariantCultureIgnoreCase)); if (exists) break; } } return exists; }
PublishingRollupImage
The internal name of Rollup Image or Upplyft bild is PublishingRollupImage
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. So if you want to check if it is a multimedia, use this id. You can see the content type id when you go to Site Actions - Site Settings - Content Types. Click on a content type you interested in. In the address bar of your browser find ?ctype=.