Loading...

Monday, September 19, 2011

OOB authorization with SPSecurityTrimmedControl

Using In Webparts
1)Declare SPSecurityTrimmedControl
SPSecurityTrimmedControl stcEditItem = new SPSecurityTrimmedControl();


2)Define Controls in WebPart
protected override void CreateChildControls()

{

stcEditItem.ID = "idPrm";

stcEditItem.Permissions = SPBasePermissions.ManageWeb;

ltrPopup.ID = "ltrPerm";

stcEditItem.Controls.Add(ltrPopup);

this.Controls.Add(stcEditItem);
}
3)Render the Trimmed Html contents,Now these content are available to administrators only


protected override void Render(HtmlTextWriter writer)

{

StringBuilder sb = AddModalPopUp(SPContext.Current.Web);

ltrPopup.Text = sb.ToString();

stcEditItem.RenderControl(writer);

}
Using in Visual Webpart
Open the .ascx file and add the controls inside SPSecurityTrimmedControl
SharePoint:SPSecurityTrimmedControl ID="ctrlSecurityTrimmed" runat="server" Permissions="ManageWeb">
controls. here..
SharePoint:SPSecurityTrimmedControl>

'value does not fall in expected range' When assigning SPUser to ListItem


If you try to assign,
ListItem["UserField"]=SPContext.Web.CurrentUser
And you get th Error  " value does not fall in expected range"
Cause:  ListItem is in different Sitecollection
So Directly You can not assign current user.
Resolution:
SPUser differntSiteCollectionUser = differentWeb.SiteUsers.GetByEmail(currentUserMailId);-->GetmailID with SPFieldUserValue

Thursday, September 8, 2011

Modal Popup and The Notification Area

Here I am covering
        1)Passing Url to Modal Popup from Server code in .cs file
          i)add the hiddenfield control to .aspx page          
and set value in cs file
hdnUrl.Value = "../Lists/ListName/NewForm.aspx?RootFolder=&";

        2)On Click of Ok button of Modal Popup Displaying the message in notification area which is part of sp2010 framework
Add following in Javascript tags
var url = document.getElementById('').value;

var options = {

url: url

title: "title",

allowMaximize: true,

showClose: true,

dialogReturnValueCallback: refreshCallback

};

function open() { SP.UI.ModalDialog.showModalDialog(options); }

function silentCallback(dialogResult, returnValue) {

}

function refreshCallback(dialogResult, returnValue) {

SP.UI.Notify.addNotification('Successful!');

SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);

}


Invoke the function

asp:Label runat="server" ID ="lblr" Text ="Open Popup">

Friday, September 2, 2011

To Position the Modal Popup Correctly...

Override the following css class and update the refering css file
.ms-dlgContent

{
position:fixed !important;
}
or use CSSRegistration.Register("_/layoout%Path%..") method (in CreatechildControl for Webpart/Prerender for usercontrol).

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;

SPSecurity.RunWithElevatedPrivileges(delegate()

{

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)

{
base.WebProvisioned(properties);

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

{

web.AllowUnsafeUpdates = true;

SPFolder rootFolder = web.RootFolder;

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

rootFolder.Update();

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;

else

return SPContext.Current.Web.CurrentUser;



}