Tuesday, August 16, 2011

Check if current user is member of Sharepoint Group in Elevated Privileges Code Block

SPUserToken userToken = SPContext.Current.Web.CurrentUser.UserToken;



using (SPSite site = new SPSite(weburl, userToken))

using (SPWeb web = site.OpenWeb())


SPGroup group = web.SiteGroups[];

if (!web.IsCurrentUserMemberOfGroup(group.ID))



Friday, August 5, 2011

WebEventReceivers on Publishing Custom Site Template

To Get the Publishing custom site template
type in browser   http://server:port/sites/publishingsite/_layouts/savetmpl.aspx
and save the template.

Prerequisites for Restoring Publishing Custom Site Template without errors
 1)Error for activating feature with specific guid
    If  you restore the Publishing template on  any other non-publishing site collection ,you will get the error for activating feature with specific guid.
To avoid this ,restore this site template in publishing site collection only.

Still if you are getting the error,
Open the WSP Package in Visual Studio 2010
Search for Onet.xml in the solution.
Ctrl+F Find the featureId for which you are getting error.
Activate it in Parent site collection and You are done.

2)Web Page not found error when subsite is created using publishing custom site template.
Write the event Web Event Receiver to set the welcome Page

public class EventReceiver1 : SPWebEventReceiver


public override void WebProvisioned(SPWebEventProperties properties)


SPWeb web = properties.Web;
if (properties.Web.MasterUrl.Contains("MasterPageNameofcustomTemplate") )


web.AllowUnsafeUpdates = true;

SPFolder rootFolder = web.RootFolder;

rootFolder.WelcomePage = "sitepages/customwelcomePage.aspx";


web.AllowUnsafeUpdates = false;



If you know the better approach to attach event receiver to particular site template only please add the comment

Thursday, August 4, 2011

A Method To Get Selected Value in spuc:PeopleEditor control (Quickly)

-- Add Control in ascx

AllowEmpty="true" MultiSelect="false" SelectionSet="User" />
-- Get User value in ascx.cs
public SPUser GetUser(PeopleEditor p)


PickerEntity entPropMgr = (PickerEntity)p.ResolvedEntities[0];

SPFieldUserValue Proposalmgrvalue = new SPFieldUserValue(SPContext.Current.Web, Convert.ToInt16(entPropMgr.EntityData[PeopleEditorEntityDataKeys.UserId]), entPropMgr.Description);

if (Proposalmgrvalue != null)

return Proposalmgrvalue.User;


return SPContext.Current.Web.CurrentUser;


Wednesday, August 3, 2011

SPDispose Check Results

- I have noticed that objects(SPWeb) created inside for loops are not being disposed. These objects need to be disposed eventhough you main block of code is "Using".

- Also when you use following snippet it will not dispose SPSite object:
using(SPWeb oWeb=new SPSite("http://abc").openWeb())



Here SPSite object is not disposed so follow below style in case of such scenerios:

using(SPSite oSite=new SPSite("url"))


using(SPWeb oWeb=oSite.OpenWeb())





This will solve the problem of disposing.
- Do not use "Using" when you work with SPContext. As SPContext are disposed automatically and if you use withing Using block than it will dispose SPWeb object and page will not work.