What is the Master Page and Use of it?
Master page provides layouts for set of pages. Master Pages are a
template that other pages can inherit from to keep consistent functionality.
The pages that inherit from Master Pages are referred to as content pages.
In SharePoint, when we add a new page from browser by going to
Site Actions and select Create Page then there we will select the page layout
and create the page. So we are selecting the page layout, it’s always binds to
a master page. Whenever you change the default or custom master page, then the
changes will be applied to all the page layouts by default. Because SharePoint
won’t give direct master page URL or name anywhere. It will be
~masterurl/custom.master for custom master page and ~masterurl/default.master
for the default master page.
Types of Master Pages in SharePoint?
There are 3 types of Master Pages in SharePoint
System Master Pages-are used for Non-Publishing Site and
Subpages(list or libraries) in Publishing Site
Site Master Pages-are usually used in Publishing Site Main Page or
sub sites
Application Master Pages are all Layout Pages.Layout pages are SharePoint administrartive pages
Application Master Pages are all Layout Pages.Layout pages are SharePoint administrartive pages
Difference between SPList TryGetList and Web.List
1.SPList list=web.List.TryGetList("listName")
Here "listName" is the name of the list and returns a SPList object.But if list is not available it will not through any exception.It will return null value.
2.SPList list=web.list["listName"]
But here if the list does not exists then it will throw an exception.
Download it:
https://sites.google.com/site/sasivalipireddy/home/RunWithElevatedPrivileages_ALL.pdf
Difference between RunWithElevatedPrivileges Vs SPUserToken:
What is the need of defining SPSite, SPWeb objects especially in RunWithElevatedPrivileges block?
If you use instances of SPSite or SPWeb, obtained prior to the RunWithElevatedPrivileges block, it won't work as expected because they are already associated to a non-elevated security context[ means current logger user]
Why can’t we use SPContext.Current.Web inside RunWithElevatedPrivileges:
Recommended practice #1:
Best recommended practice #2:
RunWithElevatedPrivileges” in Feature Receivers:
Best recommended practice #2:
Best recommended practice #3:
Here "listName" is the name of the list and returns a SPList object.But if list is not available it will not through any exception.It will return null value.
2.SPList list=web.list["listName"]
But here if the list does not exists then it will throw an exception.
All ways of RunWithElevatedPrivileges
Download it:
https://sites.google.com/site/sasivalipireddy/home/RunWithElevatedPrivileages_ALL.pdf
- RunWithElevatedPrivileges?
- Why can’t we use RunWithElevatedPrivileges in event handlers?
- Impersonation Improvements in SharePoint 2010 Event Receivers?
- Best recommended practice use of it?
- Best recommended practice to use of it in Event Receivers?
- Best recommended practice to use of it in Feature Receivers?
- RunWithElevatedPrivileges in visual studio workflows:
- Is RunWithElevatedPrivileges allowed in sandbox solution?
- By using which credentials the RunWithElevatedPrivileges will run?
- Difference between SPSecurity.CodeToRunElevated and SPSecurity. RunWithElevatedPrivileges?
Impersonation:
Impersonation is the process of executing code in the context (or on behalf) of another user identity.
What are the Impersonation methods in SharePoint 2010?
- RunWithElevatedPrivileges to impersonate as System Account user
- Passing User Token inside SPSite to impersonate as particular user
- Using Windows API
The SPSite object takes an SPUserToken object in its constructor in order to support impersonation.To impersonate the system, use the SystemAccount.UserToken property of the current SPSite context, such as:
var site = new SPSite(SPContext.Current.Site.ID, SPContext.Current.Site.SystemAccount.UserToken);
var site = new SPSite(SPContext.Current.Site.ID, SPContext.Current.Site.SystemAccount.UserToken);
Difference between RunWithElevatedPrivileges Vs SPUserToken:
- RunWithElevatedPrivileges to impersonate as System Account user
- Passing User Token inside SPSite to impersonate as particular user
As per the msdn: Executes the specified method with Full Control rights even if the user does not otherwise have Full Control.
Whenever we use SPSecurity.RunWithElevatedPrivileges (), it will execute the code under the context of Application Pool identity, so you must ensure that the App Pool account is a member of a site collection group with sufficient perms to add/edit/delete or whatever your code is trying to do. If not, the code will quietly break without popping an exception.
Method: Microsoft.SharePoint. SPSecurity.RunWithElevatedPrivileges
The code will not run within the elevated privilege if the object accessed was not created within the SPSecurity.RunWithElevatedPrivileges block. The main reason for doing so is to ensure that all the objects are in the context of the App Pool's identity.
SPSecurity.RunWithElevatedPrieveleges, there are too much tricks that you should take care of.
For Instance: You must create the new SPSite objects inside the delegate because SPSite objects created outside do not have Full Control even when referenced inside the delegate.
RunWithElevatedPrivileges does not work when HTTPContext is null:
RunWithElevatedPrivileges don’t work when HTTPContext (SPContext to be more specific) is null. So, you will not have elevation of privilege when using RunWithElevatedPrivileges in Console Application, Workflow, Timer Job or Event handlers not initiated by a request in browser.
SPSecurity.RunWithElevatedPrieveleges, there are too much tricks that you should take care of.
For Instance: You must create the new SPSite objects inside the delegate because SPSite objects created outside do not have Full Control even when referenced inside the delegate.
RunWithElevatedPrivileges does not work when HTTPContext is null:
RunWithElevatedPrivileges don’t work when HTTPContext (SPContext to be more specific) is null. So, you will not have elevation of privilege when using RunWithElevatedPrivileges in Console Application, Workflow, Timer Job or Event handlers not initiated by a request in browser.
If you use instances of SPSite or SPWeb, obtained prior to the RunWithElevatedPrivileges block, it won't work as expected because they are already associated to a non-elevated security context[ means current logger user]
Why can’t we use SPContext.Current.Web inside RunWithElevatedPrivileges:
SPContext.Current.Web can not be used directly with in the RunWithElevatedPrivileges block as the SPWeb object becomes a instance of current logged-in user's context and it gives the below error if tries to update any content in the same Web with READ only access.
Error : Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
To address the issue, a new instance of SPSite and SPWeb should be cerated within the RunWithElevatedPrivileges code block as above.
Error : Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
To address the issue, a new instance of SPSite and SPWeb should be cerated within the RunWithElevatedPrivileges code block as above.
Impersonation Improvements in SharePoint 2010 Event Receivers?
Instead of using RunWithElevatedPrivileges, In SharePoint 2010, there are new properties namely OriginatingUserToken, UserDisplayName and UserLoginName which help the developers to revert back to the original user who triggered the event very easily.
I will update once get the clear idea on what use of the new properties OriginatingUserToken, UserDisplayName and UserLoginName introduced in SharePoint 2010.
RunWithElevatedPrivileges in visual studio workflows:
No need to use any elevated privileges when working with workflows because it runs under SharePoint System Account by default (the App Pool account).
Is RunWithElevatedPrivileges allowed in sandbox solution?
You cannot use SPSecurity.RunWithElevatedPrivileges method in case of Sandboxed solution. The main reason is Sandbox solutions execute in User Code service with limited privileges.
Best recommended practice use of it:
Can’t use SPContext inside the code being RunWithElevatedPrivileges. We may get “access denied” error if instead write the SPWeb site = SPContext.Current.Web inside the RunWithElevatedPrivileges, because your web was created in the context of the current user.
Best recommended practice is: Take the current context outside the SPSecurity.RunWithElevatedPrivileges block and then create a new instance of SPSite and SPWeb inside the block which will run under application pool identity.
Simply to say is: The objects you’re working with need to be recreated within your RunWithElevatedPrivileges code block.
Recommended practice #1:
- private void Test()
- {
- Guid webID = SPContext.Current.Web.ID;
- Guid siteID = SPContext.Current.Site.ID;
- SPSecurity.RunWithElevatedPrivileges(delegate()
- {
- using (SPSite site = new SPSite(siteID))
- {
- using (SPWeb web = site.OpenWeb(webID))
- {
- // Code Using the SPWeb Object goes here
- }
- }
- });
- }
- private void Test()
- {
- SPSite site = SPContext.Current.Site;
- SPWeb web = SPContext.Current.Web;
- SPSecurity.RunWithElevatedPrivileges(delegate()
- {
- using (SPSite CurrentSite = new SPSite(site.ID))
- {
- using (SPWeb CurrentWeb = CurrentSite.OpenWeb(web.ID))
- {
- // Code Using the SPWeb Object goes here
- }
- }
- });
- }
- [Guid("b321499d-9b43-410e-8a8f-779ffb81d738")]
- public class Feature1EventReceiver : SPFeatureReceiver
- {
- public override void FeatureActivated(SPFeatureReceiverProperties properties)
- {
- try
- {
- using (SPSite spSite = properties.Feature.Parent as SPSite)
- {
- using (SPWeb spWeb = spSite.OpenWeb())
- {
- SPSecurity.RunWithElevatedPrivileges(delegate()
- {
- //code here
- });
- }
- }
- }
- catch (Exception ex)
- {
- }
- }
“RunWithElevatedPrivileges” in Event Receivers: Better to use the ID properties of the properties object, to get new instances of SPSite, SPWeb and SPListItem. If you need to run actions with elevated privileges on SPSite and SPWeb object use new SPSite(properties.SiteId); and site.OpenWeb(properties.RelativeWebUrl) instead of properties.web and web.site;
Best Recommended Practice #1- namespace MyCustomDlgFramework.EventReceiver
- {
- /// <summary>
- /// List Item Events
- /// </summary>
- public class EventReceiver : SPItemEventReceiver
- {
- /// <summary>
- /// An item is being added.
- /// </summary>
- public override void ItemAdding(SPItemEventProperties properties)
- {
- SPSecurity.RunWithElevatedPrivileges(delegate()
- {
- using (SPSite site = new SPSite(properties.SiteId))
- {
- using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
- {
- //code here
- }
- }
- });
- }
- }
- }
- namespace MyCustomDlgFramework.MyEventReceiver
- {
- /// <summary>
- /// List Item Events
- /// </summary>
- public class MyEventReceiver : SPItemEventReceiver
- {
- /// <summary>
- /// An item is being added.
- /// </summary>
- public override void ItemAdding(SPItemEventProperties properties)
- {
- using (SPSite site = new SPSite(properties.WebUrl))
- {
- using (SPWeb web = site.OpenWeb())
- {
- SPSecurity.RunWithElevatedPrivileges(delegate()
- {
- //Code here
- });
- }
- }
- }
- }
- }
- namespace MyCustomDlgFramework.EventReceiver
- {
- /// <summary>
- /// List Item Events
- /// </summary>
- public class EventReceiver : SPItemEventReceiver
- {
- /// <summary>
- /// An item is being added.
- /// </summary>
- public override void ItemAdding(SPItemEventProperties properties)
- {
- SPSecurity.RunWithElevatedPrivileges(delegate()
- {
- using (SPWeb web = properties.OpenWeb())
- {
- //Code here
- }
- });
- }
- }
- }
Chances of getting “Access Dined” error:
Note: Elevation of privilege occurs only if new SPSite created inside the block :
Note: Elevation of privilege occurs only if new SPSite created inside the block :
- private void Test()
- {
- SPSecurity.RunWithElevatedPrivileges(delegate()
- {
- SPWeb currentWeb = SPContext.Current.Web;
- SPList spList = currentWeb.Lists["MyList"];
- });
- }
- private void Test()
- {
- SPSecurity.RunWithElevatedPrivileges(delegate()
- {
- using (SPSite currentSite = new SPSite(SPContext.Current.Site.Url))
- {
- using (SPWeb currentWeb = currentSite.OpenWeb())
- {
- // Access granted as System account!!
- }
- }
- });
- }
- private void Test()
- {
- SPSecurity.RunWithElevatedPrivileges(delegate()
- {
- SPSite site = SPContext.Current.Site;
- SPWeb web = SPContext.Current.Web;
- web.AllowUnsafeUpdates = true;
- SPList list = web.Lists["MyList"];
- SPListItem item = list.GetItemById(1);
- item["MyField"] = "SharePoint";
- item.Update();
- web.AllowUnsafeUpdates = false;
- });
- }
When/where/why use "AllowUnsafeUpdates" in SharePoint?
As per msdn article “AllowUnsafeUpdates is set to true when you are trying to update the database as a result of the GET request”.
E.g. Say you have a list and you want to update something to the SharePoint data [content DB], then you need to set AllowUnsafeUpdates = true for the web and after you have done you need to set it back to false.
What will happen if you set as AllowUnsafeUpdates to false? And what is preventing it from cross site scripting attacks? Yes, the FormDigest control is taking care of all about it. I have already explained about “FormDigest’ control in previous post. Please go through to get more on it here.
To GET the content from the content DB, we need to set the AllowUnsafeUpdates = true.
To POST the content to the content DB, not require to set the AllowUnsafeUpdates = true. why means because of "FormDigest" control placed in every master page.
For reasons of security, by default, Microsoft SharePoint Foundation does not allow you to make posts from a Web application to modify the contents of the database unless you include security validation on the page making the request. You can update data for a single site or for a site collection by adding a page directive and a FormDigest control to the page that makes the request.
How to do manipulation to the SharePoint data programmatically?
So if you need to allow your code to make some updates, you need to set allow unsafe updates to true and then back to false as soon you update.
SPList list= web.Lists["list name"];
SPListItemCollection items= list.GetItems();
web.AllowUnsafeUpdates = true;
foreach (SPListItem item in items)
{
item["Field"] = "new val";
item.Update();
}
web.AllowUnsafeUpdates = false;
Remembering point on "AllowUnsafeUpdates"
- You can set it true to true to avoid security validation before the changes are written to the content database.
- GET Request needs "AllowUnsafeUpdates" enabled before updating the sharepoint objects.
- POST request doesn't need "AllowUnsafeUpdates" before updating in sharepoint objects.
- updates are currently disallowed on GET requests. To allow updates on GET, set the"AllowUnsafeUpdates" property to true.
- Always set AllowUnsafeUpdates back to true after you break inheritance in an environment with HTTPContext.
- What I have learned the hard way today is that when you set the AllowUnsafeUpdates property to true and your code jumps to the catch block of a try/catch statement that property will be set to false again.
- Remember if you inherit the page from WebPartPage class then no need to use AllowUnsafeUpdates.
Please refer to know more on “AllowUnsafeUpdates” with some real-time examples here:
Static Web part Vs. Dynamic Web part
Static Web part Vs. Dynamic Web part: A Web Part zone typically contains one or two Web Parts. However, Web Parts do not have to be placed into a zone on the Web Part Page. Static Web part Dynamic Web part Web Parts that are placed outside of a Web Part zone are called static Web Parts. Web Parts that are contained within a Web Part zone are called dynamic Web Parts. They are treated and act just like a standard Web control. They are treated and act just like a standard Web part. A static Web Part and its properties are stored within the Web Part Page (.aspx) file. The properties of a dynamic Web Part are saved to a content database. A user cannot interact with a static Web Part or modify it within a browser. Users can access the properties and manipulate these properties.
No comments:
Post a Comment