Exception Handling
When we use the SharePoint API, exceptions may occur when getting some objects. How does com handle this situation.
When we get a list, the Code is as follows:
Using (clientcontext = new clientcontext ("https://cnblogtest.sharepoint.com") {var pasword = new securestring (); "[email protected] #". tochararray (). tolist (). foreach (pasword. appendchar); clientcontext. credentials = new sharepointonlinecredentials ("[email protected]", pasword); // sets the permission var currentweb = clientcontext. web; // when calling this API, if the list does not exist on the server side, an exception occurs. VaR list = currentweb. Lists. getbytitle ("Documents test"); clientcontext. Load (list); clientcontext. executequery (); // execute the query and return an exception}
If an exception occurs on the server side, the server returns the exception information to the com side through the JSON object. Here, let's take a look at the exception response:
The JSON object explicitly displays the exception information returned when getlistbyid is executed.
If the list is obtained through currentweb. Lists. getbytitle ("Documents test"), an exception occurs if the list does not exist on the server side. We only need to try catch when writing server-side applications, but we know that clientapi calls are called through WCF, we only know whether exceptions occur on the server side after executing executequery, and then the server packs the exception information and returns it to com.
This API behavior will bring us a series of problems. If the list does not exist, we need to create a new one. What if an exception is not thrown by com? In the above example, if we create a new list when the list does not exist, we still need a request. How can we implement it in another request?
Use exceptionhandlingscope to improve com Program Performance
This class is a common class used to handle server exceptions. In the above example, we can use the following code.
Using (clientcontext = new clientcontext ("https://cnblogtest.sharepoint.com") {var pasword = new securestring (); "[email protected] #". tochararray (). tolist (). foreach (pasword. appendchar); clientcontext. credentials = new sharepointonlinecredentials ("[email protected]", pasword); // sets the permission var currentweb = clientcontext. web; var exceptionhandlingscope = new exceptionhandlingscope (clientcont EXT); // list = NULL; using (VAR currentscope = exceptionhandlingscope. startscope () {using (exceptionhandlingscope. starttry () {// when calling this API, if this list does not exist on the server side, an exception will occur. VaR listgetbyid = currentweb. lists. getbytitle ("Documents test"); listgetbyid. description = "list get by ID"; listgetbyid. update ();} using (exceptionhandlingscope. startcatch () {listcreationinformation listcreationinfo = new listcreationinformation (); listcreationinfo. title = "Documents test"; listcreationinfo. templatetype = (INT) listtemplatetype. documentlibrary; listcreationinfo. description = "list create in Catch Block"; currentweb. lists. add (listcreationinfo) ;}} list = currentweb. lists. getbytitle ("Documents test"); clientcontext. load (list); clientcontext. executequery (); // execute the query. No exception occurs. // check whether the console is abnormal on the server. writeline ("server has exception:" + exceptionhandlingscope. hasexception); // server exception information console. writeline ("server error message:" + exceptionhandlingscope. errormessage );}
This class solves the problem we mentioned above. during compilation, the server will compile the code in exceptionhandlingscope into a try catch code, therefore, we can implement a logic like try catch through a single request.
The request message after the above Code has been compiled by COM is:
<Request addexpandofieldtypesuffix = "true" schemaversion = "15.0.0.0" libraryversion = "15.0.0.0" applicationname = ". Net Library"
Xmlns = "http://schemas.microsoft.com/sharepoint/clientquery/2009">
<Actions>
<Objectpath id = "2" objectpathid = "1"/>
<Objectpath id = "4" objectpathid = "3"/>
<Exceptionhandlingscope id = "5">
<Tryscope id = "7">
<Objectpath id = "10" objectpathid = "9"/>
<Objectpath id = "12" objectpathid = "11"/>
<Objectidentityquery id = "13" objectpathid = "11"/>
</Tryscope>
<Catchscope id = "15">
<Objectpath id = "18" objectpathid = "17"/>
<Objectidentityquery id = "19" objectpathid = "17"/>
</Catchscope>
</Exceptionhandlingscope>
<Query ID = "22" objectpathid = "11">
<Query selectallproperties = "true">
<Properties/>
</Query>
</Query>
</Actions>
<Objectpaths>
<Staticproperty id = "1" typeid = "{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" name = "current"/>
<Property id = "3" parentid = "1" name = "Web"/>
<Property id = "9" parentid = "3" name = "Lists"/>
<Method id = "11" parentid = "9" name = "getbytitle">
<Parameters>
<Parameter type = "string"> parameters test </parameter>
</Parameters>
</Method>
<Method id = "17" parentid = "9" name = "add">
<Parameters>
<Parameter typeid = "{e247b7fc-095e-4ea4-a4c9-c5d373723d8c}">
<Property name = "customschemaxml" type = "null"/>
<Property name = "performanceproperties" type = "Dictionary"/>
<Property name = "Description" type = "null"/>
<Property name = "documenttemplatetype" type = "int32"> 0 </property>
<Property name = "quicklaunchoption" type = "Enum"> 0 </property>
<Property name = "templatefeatureid" type = "guid"> {00000000-0000-0000-0000-000000000000} </property>
<Property name = "templatetype" type = "int32"> 101 </property>
<Property name = "title" type = "string"> documents test </property>
<Property name = "url" type = "null"/>
</Parameter>
</Parameters>
</Method>
</Objectpaths>
</Request>
From this packet, We can roughly see how com represents this try catch finally code block. We can see that exceptionhandlingscope also has the same ID information as clientobject, so that the server side can compile the corresponding code into try catch finally on the server side.
Since COM is based on HTTP requests, it is important to write efficient programs to minimize the number of requests.
Use exceptionhandlingscope for efficient SharePoint COM programming