Various context that may be used in SharePoint Development)

Source: Internet
Author: User
Tags microsoft website

The computer is installing office component for Visual Studio 2012 from the Microsoft website at a very slow speed. It seems that a job tonight cannot be done. Simply write a blog.

Sharepoint is a B/S structure product, so a variety of context information will be used in the development process, so I would like to take this opportunity to summarize.

I. httpcontext

This ...... I don't want to introduce it any more. SharePoint runs in standard ASP. under the. NET Framework (2003 is not a standard ASP. NET 1.1, but there are still 2003 people in this year), so the use of this object and ASP. net.

 

Ii. spcontext

It can be seen from the name that this is the context object of SharePoint itself. In addition to encapsulating httpcontext, it provides a lot of context information related to SharePoint.

Spcontext contains the most frequently used static attribute: spcontext. current, used to obtain the current context information (and httpcontext. current is similar, if the program runs on the web, you can use this ). I have met many developers who are new to SharePoint. When writing webpart, they are still using the new spsite method to obtain the current website set. This is unnecessary, it is also a waste of server resources (spsite and spweb objects all contain unmanaged resources, while site and web in current are managed by Sharepoint's runtime environment and can be quickly accessed ).

Next, let's take a look at the common attributes contained in this spcontext that can be used during development (some attributes are actually complicated in internal logic. Here we will briefly introduce some basic scenarios ):

Attribute Description
Site Spsite type, frequently-used attributes, current website set
Web Spweb type, the most common attribute, the current website.
Many programs start with spcontext. Current. web ......
LIST/listid Splist/GUID type, current list.
You can use this attribute to obtain the current list on the view page, form page, or set page of the list. In fact, as long as there is a valid list parameter in the URL query, the parameter value can be the guid of the list.
Listitem/Itemid Splistitem/int32 type, the current list entry.
It is generally used to view and edit a list (of course, you can create a new form, but there is no ID), or to obtain the corresponding entries on the current page in the page library. In addition, if the current list attribute is valid, you can use this attribute to obtain corresponding entries as long as there is a valid ID parameter in the URL query.
Listitemversion Splistitemversion: version of the current list entry.
If you are viewing a version entry from the previous version view page, you can use this attribute to obtain the corresponding version on that view page.
Listitemdisplayname/
Listitemserverrelativeurl
String/string type, the display name of the current entry (if it is a normal list, it is the value of the title field; if it is a document library, it is a file name without the extension; or a folder name ), and URL.
File Spfile type. If the current entry is a file, this attribute can directly obtain the file object.
Equivalent to spcontext. Current. listitem. File
Rootfolderurl String type. The URL address of the current view.
If the current list view is in a list sub-folder, you can use this attribute to get the address of this folder; otherwise, it is the root folder address of the current list.
Ispopui Boolean Type to determine whether the current page is in the dialog box.
Only the dialog box of SharePoint 2010, excluding the modal dialog box of the browser.
Formcontext Spformcontext type, current list form context (used for list form pages)
These attributes are generally used:
Formmode: Form type, new/edit/display
Fieldcontrolcollection: a set of field controls on a form
Viewcontext Spviewcontext type, current view context (used for list view pages)
These attributes are generally used:
View: spview object, current view
Viewid: View guid
Contextpageinfo Spcontextpageinfo type, current page context (used for pages in the page Library)
These attributes are generally used:
Listid: ID of the document library where the page is located
Itemid: ID of the current page as a list entry
Basepermissions: the current user's permissions on the current page
Iswebwelcomepage: whether the current page is the homepage of the website

 

Iii. "CTX" in Javascript"

This is not mentioned in Microsoft SDK. On all Web parts pages containing the list view, each list view corresponds to a javascript variable of CTX [blabla, the [blabla] Following is a number, which is also the ctxid attribute of the variable. All these CTX variables are placed in the global JavaScript variable g_ctxdict. The key is the variable name string, and the value is the variable. Therefore, this global variable is convenient, you can use JavaScript to find all views on the current page.

The CTX variable is a JavaScript "class" called contextinfo in name. It contains many attributes and is commonly used as follows:

Attribute Description
Ctxid A number that identifies this variable, such as 185, is ctx185. You can use ctx185 or g_ctxdict ['ctx185'] to find this variable (in some cases, there is a simpler method, which will be discussed later ). It should be noted that this identifier may change every time you refresh the page. I will go deep into the specific principle for the moment.
Listbasetype The basic type of the list, which is equivalent to the basetype attribute of splist.
Listtemplate The template ID of the List (for example, the document library is 101 and the notification is 104), which is equivalent to the basetemplate attribute of the splist.
Listname List ID. Do not confuse variable names.
View ID of the List View
Listurldir The root path of the list, which is equivalent to splist. rootfolder. serverrelativeurl.
Httproot Root path of the current website (absolute path)
Sitetitle Current website title
Listtitle The title of the List (this is the title, the listname is not)
Currentuserid Current User ID
Wpq [*] The ID of the webpart in the rendering view, such as wpq2.
Listdata [*] This is amazing. This attribute points to an array, which is the list entries displayed in the current view, including the values of each field, in JSON format.
Listschema [*] The definition of each field in the list (including the field name and field type) in JSON format.
Basepermissions [*] The basic permissions of the current user on this list, as shown in the following figure:
{Managelists: True, managepersonalviews: True, openitems: true}

The attributes added with [*] are newly added to SharePoint 2013.

In fact, there is a variable named "CTX" on the page that contains the list view, which points to the last CTX [blabla] variable on this page, therefore, if there is only one list view on the page, CTX can be used directly. For example, if you want to check the list template ID on a list view page, you can directly enter javascript: Alert (CTX. listtemplate) in the address bar of the browser.

The main function of the CTX variable is to generate the drop-down menu (ECB-Edit Control Block) of the list item. If you go to corev4.js, we can see that CTX is passed as a parameter when the drop-down menu is created. If you need to customize the list item menu, in addition to using feature, or the newly added method of adding custom action through SPD, you can also use the JavaScript method that continued from the 2010 era: add the custom_addlistmenuitems method or the custom_adddoclibmenuitems Method to the page (you can search for it by yourself). CTX is one of the parameters of the two methods.

In addition, as an important JavaScript variable in the List View, CTX can be used in some embedded scripts on the page to accomplish various evil tasks.

 

4. The _ sppagecontextinfo variable in Javascript

It is also a non-document JavaScript variable. Although the name is similar to a server type mentioned above, this JavaScript variable contains more content than spcontextpageinfo, it includes the following common attributes:

Attribute Description
Webserverrelativeurl The relative server path of the website (starting)
Currentlanguage Lcid of the current website
Webuiversion The UI style of the current website (the 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 serves as the ID of the List entry
Webabsoluteurl [*] Absolute website path (starting with "http: //" or "https)
Siteabsoluteurl [*] Absolute path of the website set (starting with "http: //" or "https)
Layoutsurl [*] Layouts's relative server path (2013 actually has two layouts root paths, one is 15 and the other is 14)
Webtitle [*] Website title
Webpermmasks [*] Permissions of the current user on the current website, such as: {high: 2147483647, low: 4294967295}
(Because JavaScript does not support 64-bit integers, the high position and position are separated)
Siteserverrelativeurl [*] The relative server path of the website set (starting)

The attributes added with [*] are newly added to SharePoint 2013.

_ Sppagecontextinfo: The main use case of the variable is to obtain some basic information about the current page in Javascript, especially in the release page, there will be a lot of use value.

 

V. Other global JavaScript Variables

In fact, there are some global JavaScript variables on the page, and some context information can be obtained:

Variable Description
G_wsalcid The current language, equivalent to _ sppagecontextinfo. currentlanguage
G_wsasitetemplateid The website template used by the current website. For example, "STS #1" indicates the workgroup website.
_ Spuserid ID of the current user. The javascript variable is actually rendered by the welcome menu in the upper right corner.
_ Spwebpermmasks The current user's permissions on the current website, equivalent to _ sppagecontextinfo. webpermmasks

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.