Go Various context (contexts) that may be used in SharePoint development

Source: Internet
Author: User

SharePoint is a B/s structure of the product, so in the development process will use a variety of context information, take this opportunity to summarize. Especially JavaScript's CTX is very practical, here to record!

First, HttpContext

This one...... I don't want to mention that SharePoint runs under the standard ASP. NET Framework (2003 is not the standard ASP. NET 1.1, but there are still 2003 of them this year), so there is no difference between the use of this object and the ASP.

Second, SPContext

As you can see from the name, this is SharePoint's own context object, which provides a lot of contextual information about SharePoint, in addition to encapsulating the HttpContext.

SPContext contains a static property that we use most often: spcontext.current, which is used to get the current context information (similar to HttpContext.Current, which can be used if the program is running on the web). I've seen a lot of people who are beginners of SharePoint development who are also using new SPSite to get the current site collection when writing WebPart, which is not necessary and a waste of server resources (SPSite, SPWeb objects contain unmanaged resources, The site and the Web in current are managed by SharePoint's own runtime environment and can be quickly taken.

Let's take a look at what common properties are included in this spcontext, which can be used during development (some properties are actually very complex inside logic, here's a brief introduction to some basic scenarios):

Property Description
Site SPSite type, secondary commonly used property, current site collection
Web The SPWeb type, the most commonly used property, is the current Web site.
A lot of programs start with SPContext.Current.Web ...
List/listid Splist/guid type, current list.
Whether you are on the view page of the list, on the form page, or on the Settings page, you can get the current list from this property. In fact, as long as the URL query inside a valid list parameter, the value of the parameter is the GUID of the list can be.
Listitem/itemid The Splistitem/int32 type, the current list entry.
Generally used for list viewing and editing forms (of course, a new form is actually possible, except that there is no ID), or to get entries for the current page that is stored in the page library. In addition, if the current list property is valid, you can use this property to get the corresponding entry as long as there is a valid ID parameter in the URL query.
Listitemversion The version of the Splistitemversion type, which corresponds to the current list entry.
If you are viewing a version of an entry from the Historical version view page, you can use this property to get to the corresponding version directly on that view page.
Listitemdisplayname/
Listitemserverrelativeurl
The string/string type, the display name of the current entry (if it is a normal list, is the value of the title field, if it is a document library, a file name without an extension, or a folder name), and a URL.
File SPFile type, this property can directly get the file object if the current entry is a file.
Equivalent to SPContext.Current.ListItem.File
RootFolderURL The string type that corresponds to the URL address of the current view.
If the current list view is in a subfolder of the list, you can get the address of the folder by this property, or the root folder address of the current list.
Ispopui A Boolean type that determines whether the current page is in a dialog box.
SharePoint 2010 Only dialog box, not including the browser's modal dialog box.
Formcontext Spformcontext type, current list form context (for List form pages)
You can generally use several properties:
Formmode: Form type, New/edit/display
Fieldcontrolcollection: A collection of field controls on a form
ViewContext Spviewcontext type, current view context (for List view pages)
You can generally use several properties:
View:spview object, Current View
ViewId: GUID of view
Contextpageinfo Spcontextpageinfo type, current page context (for pages in page library)
You can generally use several properties:
ListId: ID of the document library where the page resides
ItemId: The current page as the ID of the list entry
BasePermissions: Current user's permissions on the current page
Iswebwelcomepage: Whether the current page is the homepage of the site

Third, the "CTX" in JavaScript

This is not mentioned in the Microsoft SDK, in all the Web Parts page containing the list view, each list view corresponds to a ctx[blabla] JavaScript variable, the latter [BlaBla] is a number, which is the Ctxid property of the variable. These CTX variables are placed in a global JavaScript variable g_ctxdict This object, its key is the variable name string, value is the variable, so to facilitate this global variable, you can find all the views of the current page through JavaScript.

The CTX variable is nominally a JavaScript "class" called ContextInfo, which contains a very rich set of properties that are commonly used as follows:

Property Description
Ctxid A number that identifies this variable, such as 185, then this variable is ctx185. You can use ctx185 or g_ctxdict[' ctx185 ' to find this variable (in some cases there's a simpler way to do it later). Note that each time you refresh the page, the logo may be changed, the specific principle of my temporary wood to go deep.
Listbasetype The underlying type of the list, equivalent to the BaseType property of SPList
ListTemplate The template ID of the list (for example, the document library is 101, the notification is 104), which is equivalent to the Basetemplate property of SPList
ListName The ID of the list, not to be confused by the variable name
View The ID of the list view
Listurldir The root path of the list, equivalent to SPLIST.ROOTFOLDER.SERVERRELATIVEURL
Httproot Root path of the current Web site (absolute path)
Sitetitle Title of the current Web site
ListTitle The title of the list (this is the title, ListName not)
Currentuserid ID of the current user
Wpq[*] Render view the ID of the WebPart, such as WPQ2
Listdata[*] That's a good thing, this property points to an array, which is the list entry shown in the current view, containing the values of each field, in JSON format.
Listschema[*] The definition of each field in the list (including field names, field types, and so on), in JSON format.
Basepermissions[*] The current user's basic permissions on this list look like this:
{managelists:true, managepersonalviews:true, openitems:true}

The ones that follow [*] are the new properties of SharePoint 2013.

In fact, in the page containing the list view, there is a variable named "CTX", which points to the last Ctx[blabla] variable on the page, so if there is only one list view in the page, you can use CTX directly. For example, in a list view page, you want to see the list template ID is how much, you can directly in the browser address bar input: Javascript:alert (ctx.listtemplate), uh.

The most important role of the CTX variable is the drop-down menu (Ecb–edit Control Block) that generates the list item, and if you go through that corev4.js, you can see that CTX is passed in as a parameter when you create that drop-down menu. If you need to customize the list item menu, in addition to using feature, or the 2010 new way to add custom action via SPD, you can also use JavaScript that continues from the 2007 era: Add Custom_ to the page Addlistmenuitems method or Custom_AddDocLibMenuItems method (use your own search), and CTX is one of the parameters of these two methods.

In addition, as an important JavaScript variable in the list view, by using CTX in some of the scripts embedded in the page, you can do all sorts of things that are more sinister, and you have to play with your own imagination.

Iv. _sppagecontextinfo variables in javascript

Also a JavaScript variable without a document, although the name is similar to one of the server-side types mentioned earlier, but this JavaScript variable contains much more content than Spcontextpageinfo, it contains some of the following common properties:

Property Description
Webserverrelativeurl The relative server path of the Web site (beginning with "/")
Currentlanguage The language LCID for the current Web site
Webuiversion UI style for the current web (2010/2013 style is 4 if the master page is 2007 style is 3)
Pagelistid List ID of the page library
Pageitemid If the current page is in the page library, the current page is the ID of the list entry
Webabsoluteurl[*] Site absolute Path ("HTTP//" or "https://")
Siteabsoluteurl[*] Site collection Absolute Path ("HTTP//" or "https://" begins)
Layoutsurl[*] Layouts Relative server path (2013 actually has two layouts root path, one is 15, one is 14)
Webtitle[*] Site Title
Webpermmasks[*] Current user's permissions to the current Web site, such as: {high:2147483647,low:4294967295}
(because JavaScript does not support 64-bit integers, it does take the high and the position apart)
Siteserverrelativeurl[*] Relative server path to the site collection (beginning with "/")

The ones that follow [*] are the new properties of SharePoint 2013.

The main usage scenario of the _sppagecontextinfo variable is to get some basic information about the current page in JavaScript, especially in the publishing page, which has more useful value.

V. Other JavaScript global variables

There are also some global JavaScript variables in the page, and you can get some of the current contextual information:

Variable Description
G_wsalcid The current language, equivalent to _sppagecontextinfo.currentlanguage
G_wsasitetemplateid The site template used by the current site, such as "sts#1", represents a team site
_spuserid ID of the current user, this JavaScript variable is actually rendered in the top right-hand corner of the Welcome menu
_spwebpermmasks The current user's permissions on the current Web site, equivalent to _sppagecontextinfo.webpermmasks

Go Various context (contexts) that may be used in SharePoint development

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.