SharePoint API Test Series-OM methods operations on recorded item (the magical work of delegates)

Source: Internet
Author: User

To test the recorded item dynamic call OM methods, the interface is as follows:

Enter the site's url,file relative URL in SharePoint, select the OM action type, and click Test for testing. The code is as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingMicrosoft.SharePoint;namespacerecordsomtest{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); OMMETHODS_COMBOBOX.ITEMS.ADD ("Rename"); OMMETHODS_COMBOBOX.ITEMS.ADD ("File Item Delete"); OMMETHODS_COMBOBOX.ITEMS.ADD ("File Delete"); OMMETHODS_COMBOBOX.ITEMS.ADD ("File Item Recycle"); OMMETHODS_COMBOBOX.ITEMS.ADD ("File Recycle"); OMMETHODS_COMBOBOX.ITEMS.ADD ("File Undo Check out"); OMMETHODS_COMBOBOX.ITEMS.ADD ("File Check in"); OMMETHODS_COMBOBOX.ITEMS.ADD ("File Check out"); }         Public Delegate voidCallommethod (SPFile file); Private voidTest_button_click (Objectsender, EventArgs e) {            /*Block Delete Test. */            //Rename test.Callommethod Rename = file + {file. item["Name"] ="Kkk.txt"; file. Item.update ();            }; //Delete Test 1.Callommethod delete1 = File = ={file. Item.delete ();            }; //Delete Test 2.Callommethod Delete2 = File = ={file. Delete ();            }; //Delete Test 3.Callommethod Delete3 = File = ={file. Item.recycle ();            }; //Delete Test 4.Callommethod delete4 = File = ={file. Recycle ();            }; /*Block Edit and delete test. */            //undocheckout test.Callommethod undockout = File = ={file. UndoCheckout ();            }; //CheckIn test.Callommethod Ckin = file + {file. CheckIn ("Check in.");            }; //CheckOut test.Callommethod ckout = File = ={file. CheckOut ();            }; Switch(ommethods_combobox.text) { Case "Rename":                    //Rename test.doomtest (Rename);  Break;  Case "File Item Delete": Doomtest (delete1);  Break;  Case "File Delete": Doomtest (DELETE2);  Break;  Case "File Item Recycle": Doomtest (Delete3);  Break;  Case "File Recycle": Doomtest (DELETE4);  Break;  Case "File Undo Check out": Doomtest (undockout);  Break;  Case "File Check in": Doomtest (Ckin);  Break;  Case "File Check out": Doomtest (ckout);  Break; }        }        Private voiddoomtest (callommethod cm) {using(SPSite site =NewSPSite (Siteurl_textbox.text)) {SPWeb Web=site.                RootWeb; SPFile file=web.                GetFile (Filepath_textbox.text); Try{ cm.                    Invoke (file); //Reach This step means test failed.Result_richtextbox.text ="Test failed.\r\n"; Result_richtextbox.selectioncolor=color.red; }                Catch(Exception ex) {Result_richtextbox.text="Test passed.\r\n"; Result_richtextbox.select (0, result_richTextBox.Text.Length); Result_richtextbox.selectioncolor=Color.green; Result_richtextbox.text+=string.                Concat (ex); }             }        }    }}

Extract the public part of each OM methods unit test as a doomtest (Callommethod cm) method, in lieu of calling a delegate method ( Orange font ) for different parts of each test. Then we declare the type of the delegate method (the delegate method needs to pass in a parameter of type SPFile):

 Public Delegate void Callommethod (SPFile file);

Then instantiate the delegate according to the different parts of each test:

//Rename test.Callommethod Rename = file + {file. item["Name"] ="Kkk.txt"; file. Item.update (); };//Delete Test 1.Callommethod delete1 = File = ={file. Item.delete (); };//Delete Test 2.Callommethod Delete2 = File = ={file. Delete (); };//Delete Test 3.Callommethod Delete3 = File = ={file. Item.recycle (); };//Delete Test 4.Callommethod delete4 = File = ={file. Recycle (); };//undocheckout test.Callommethod undockout = File = ={file. UndoCheckout (); };//CheckIn test.Callommethod Ckin = file + {file. CheckIn ("Check in."); };//CheckOut test.Callommethod ckout = file + {file. CheckOut (); };

This is the magic of commissioning in testing, greatly reducing the amount of repetitive code.

SharePoint API Test Series-OM methods operations on recorded item (the magical work of delegates)

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.