Preface:
I have been busy some time ago, and I have no time to summarize it. I just launched the project before the festival and can summarize some of the problems I encountered during the holiday. The main content is to query by using the Sharepoint Server Object Model and add the callout menu for the SharePoint custom list item. Hope to help you.
1. Reference Visual Web components on the ASPX page
Sometimes you need to reference the Visual Web parts created in the project on the page. The specific steps are as follows:
1) register the visualization Web component at the top of the ASPX page
Example:
<% @ Register tagprefix = "navtree"
Namespace = "sample. webparts. navtreewebpart" assembly = "$ Sharepoint. Project. assemblyfullname $" %>
Attribute description:
L tagprefix: The tag name used by the control on the page, similar to <asp: button…> In ASP, this attribute value can be set by the user at will
L namespace: namespace for Visual Web parts
L assebly: The Assembly where the Visual Web component is located (no need to modify)
2) page reference
<Div> <navtree: navtreewebpart id = "navtree" runat = "server"/>
</Div>
Note:
The yellow highlighted part of navtree is the control label name registered above, and navtreewebpart is the visual Web Part class name.
3) Background call
You can use navtree. attributes to obtain the relevant attributes of Web Components for development.
2. Custom timer job
In some parts of the project, you need to regularly execute some tasks, so you need to create a scheduled job. The main steps are as follows:
1) Add a feature in the SharePoint solution, and right-click to add an event receiver for the feature.
The default methods in the event receiver are commented out. In these methods, you only need to uncomment the following two methods:
// Process events triggered after a function is activated.
Public override void featureactivated (spfeaturereceiverproperties properties ){...... } // Process events that occur before a feature is disabled. Public override void featuredeactivating (spfeaturereceiverproperties properties ){...... }
In the featureactivated method, the properties of the parameter can be converted to the corresponding object based on the range of the feature, and the class inherited from spjobdefinition can be instantiated in this method.
2) create a job class that inherits from the spjobdefinition class
As follows:
Public class sampletimerjob: spjobdefinition {public sampletimerjob (): Base () {}// create timer job public sampletimerjob (string jobname, spwebapplication webapp) for Web applications: Base (jobname, webapp, null, spjoblocktype. contentdatabase) {This. title = jobname;} // method to be executed when the timer job is executed public override void execute (guid targetinstanceid ){...... ...... In this method body, you can write the corresponding business logic spwebapplication webapp = This. Parent as spwebapplication ;}}
3) Call the featureactivated method in 1). 2) The created sampletimerjob class inherits from the spjobdefinition class.
The specific implementation is as follows:
// Initialize spjobdefinition sampletimerjob job = new sampletimerjob (jobname, webapp); spminuteschedule schedule = new spminuteschedule (); schedule. beginsecond = 0; schedule. endsecond = 5; schedule. interval = 10; job. schedule = schedule; job. update ();
4) after completing the preceding operations, deploy the entire project. After activating feature, you can monitor and check the job status in the management center and view the "job definition ", you can find the custom timer job.
3. query using the Sharepoint Server Object Model
The SharePoint search API provides multiple query methods. Here we mainly introduce the use of the Server Object Model query, mainly using the keywordquery class.
The Code is as follows:
Keywordquery = new keywordquery (spcontext. current. site); // when keywordquery creates an object, the parameter can also be the specified website set keywordquery. querytext = querystr; // specify the keyword keywordquery to be queried. startrow = start_row; // sets the number of rows starting from the query result. You can use this attribute to create a paging keywordquery for the query result. rowlimit = pagecount; // specify the number of returned results in keywordquery. trimduplicates = false; // whether to remove repeated items from the query results searchexecutor = new searchexecutor (); resulttablecollection = searchexecutor. executequery (keywordquery); // execute the query var resulttables = resulttablecollection. filter ("tabletype", knownabletypes. relevantresults); resulttable = resulttables. firstordefault (); datatable = resulttable. table; // query result
Note:
You can also use this class to query the value of the managed metadata. However, you need to know that the metadata field is mapped to the searched value.
Keywordquery. querytext can be used to query both keywords and managed metadata fields. It can also be used to query combinations of keywords and managed metadata fields. The format is as follows:
Querytext = "keyword"
Querytext = "Author: James ". Here, author is the managed metadata field. If there are multiple managed metadata fields, you can use and for associated query.
Querytext = "keyword Author: zhangsan". If it is a combination of a keyword and a hosted metadata field, you only need to separate it with spaces and remove the "and" string.
4. Use the callout annotation control for the SharePoint 2013 custom list item
The tag control is used in the default list item of SharePoint 2013, as shown in. How can we use this control in our custom list? The following is an example.
As shown in, The callout control consists of the title, callout action, and menu entity.
L title: used to display the title of callout
L callout action: used to perform some operations
L menu entity: equivalent to one of multiple actions in a callout action
4.1 create a callout Control
Before creating the callout control, make sure that the callout. js file is loaded as follows:
Sp.sod.exe cutefunc ("callout. JS "," callout ", function () {// create the callout control var callout = calloutmanager. createnew ({ID: "callout" + listitemid, launchpoint: targetelement, // beakorientation: "leftright", // location of the callout pop-up box title: filename, // callout title openoptions: {event: "click", // triggers callout behavior: Click showclosebutton: True // whether the callout close button is displayed}, content: filename // content to be displayed by callout });.......... .......... });
4.2 Add callout action
Here is just an example. It demonstrates adding a callout action. If you need to add multiple actions, the operation is the same
// Create a callout action var calloutaction1 = new calloutaction ({text: "view attributes", tooltip: "view attributes", isenabledcallback: function () {return true ;}, onclickcallback: function (event, Action) {alert ('view attribute'); // In this method, you can write your own logic code area to implement the desired function. Here is the example callout. close () ;}}); callout. addaction (calloutaction1); // after creating a callout action, add the action to the callout control ............. .............
4.3 Add a callout action with multiple menuentries
// Create a callout action. This action is the same as a normal action, but the menuentries attribute is added. This attribute is an array of menuentry. You can add multiple calloutactionmenuentry objects as needed, each object is equivalent to an action.
VaR calloutaction3 = new calloutaction ({text :"... ", menuentries: [New calloutactionmenuentry (" Browse File ", function () {alert (" Browse File ") ;}), new calloutactionmenuentry (" details ", function () {alert ("details") ;})]}); callout. addaction (calloutaction3); // Add the action to the callout Control
After completing the preceding operations, you can get the effect shown in the first step.
This example only demonstrates how to add a callout control to a list item. In actual use, if you need to add a callout control to a custom list item, you need to create and bind the callout Control for each list item when loading the page. Therefore, you can encapsulate the code for creating the callout control and adding the callout action into a function, execute this function when loading the page, as shown below:
Function createcallout (targetelement, title, content) {// The code here is the code sp.sod.exe cutefunc ("callout. JS "," callout ", function () {// create the callout control var callout = calloutmanager. createnew ......... .........}
After encapsulation, we also need to obtain the parameters required for this method, mainly by obtaining the attributes of the page element as the parameters of the method, where. BS-launchpoint is a special attribute of the page element. The page element HTML is as follows:
<A Title = "Open menu" class = "MS-lstitmlinkanchor MS-ellipsis-A js-callout-launchpoint BS-launchpoint" attr_filename = '<% # eval ("FILENAME ") %> 'attr_content = '<% # eval ("content ") %> '> </a>
You also need to encapsulate the method as follows:
Function initialcallout () {$ ('. BS-launchpoint '). each (function () {// you can add a special style to the list item to obtain the DOM object of the page element of this list item, and pass the parameters to the createcallout method in sequence, and call this method, createcallout ($ (this) [0], $ (this ). ATTR ('attr _ filename'), $ (this ). ATTR ('attr _ content ')});}
After completing the preceding operations, run the following code:
_ Spbodyonloadfunctions. Push (initialcallout); // This method is the js method that comes with Sharepoint. It is mainly used to register the passed method name and execute it in the onload method of the body.
Basically all the operations are completed here, so you can use the callout control in the custom list items.
5. SharePoint document follow
After the related files are found in the search Center, The callout action of the pop-up annotation control has the following item:
How can I add the "follow" feature to a custom list item?
We have used window. setfollowstatus (...) in followingcommon. js (....) Method.
As described in section 4 "add a callout control to the custom list item", you only need to add a callout action to the callout control. The specific implementation is as follows:
To use the setfollowstatus () method, you must ensure that followingcommon. JS is loaded first, as shown below,
SP.SOD.executeFunc(‘followingcommon.js‘, null, function () {window.SetFollowStatus(url, true, true);}
This method has three parameters. The first is the URL of the list item to be followed. The specific format is as follows:
URL: http: // weburl? Listid = _ listid & Itemid = _ Itemid
Set the remaining two parameters to true.
In this way, click the "follow" callout action item in the callout control of the page list item to see the "follow" callout action item. After clicking it, if the follow is successful, a prompt will be displayed on the upper right of the page. As follows:
Daily Learning Experience: Add callout menu items, document follow, and Sharepoint Server Object Model query for Sharepoint 2013 custom list items