Building scripts

Building scripts is performed via writing program code in C# or Visual Basic language. All work with scripts is done in the script building window.

When opening window of script creation, HarePoint Explorer for SharePoint generates it`s minimal program code:

C#
 
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using MAPILab.SharePoint.Explorer.CodeForm;
using MAPILab.SharePoint.Explorer.Utilities.ScriptRunner;
 
public class Tester
{
    static void Main(
         MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm thisForm
        ,MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser browser
        )
    {      
        
        
        // Output browser configuration 
        //browser.Text = "Browser window"; 
        //browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded; 
        browser.ReturnValue = null;
    }
}
Visual Basic
 
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports MAPILab.SharePoint.Explorer.CodeForm
Imports MAPILab.SharePoint.Explorer.Utilities.ScriptRunner
 
Public Class Tester
    Shared Sub Main(ByVal thisForm 
    As MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm, 
                    ByVal browser 
                    As MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser)
               
               
        ' Output browser configuration 
        'browser.Text = "Browser window" 
        'browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded 
        browser.ReturnValue = Nothing 
    End Sub 
End Class 

This code contains description of class Tester, that includes statistical method Main.

The method Main is a start point for executing the script. Signature of this method is generated automatically on basis of selected objects in browser windows. Not depending on the condition of browser windows, method Main always gets parameters thisForm and browser. Parameter thisForm contains reference to script building window itself. Object returned by method Main is displayed in new browser window (except for cases when null reference is returned).

Only name of class Tester and signature of method Main must remain unchanged in the script programming code - everything else can be changed as you like. It is possible to add descriptions of additional types, modify description of both class Tester and body of method Main, and etc.

As mentioned before, signature of method Main is generated automatically depending on the number of objects flagged in browser windows. Considering the fact that in browser window not only property or field, but also any method can be flagged, the result of generation of method main may strongly vary. To demonstrate this, let`s review two examples.

Example of use the parameter browser and its parameters

Pairmeter browser used to visualize and return one or more if necessary facilities during the execution of the script. Parameter browser has the following properties:
  1. DisplayMode - Appearance object mode. Takes the values Raw and Expanded. By default is Raw. But for the convenience of displaying such objects as collections or arrays of objects you can use Expanded.
  2. RaturnValue - Object(s) to display in the object browser window.

Let us demonstrate the use of the parameter browser to display a collection of services of SharePoint farm.

Let the browser window is marked property Services.

 

Services
Click to open real size screenshot


Create a new script window and execute the following code, which returns a collection of services in Raw mode:
C#
 
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using MAPILab.SharePoint.Explorer.CodeForm;
using MAPILab.SharePoint.Explorer.Utilities.ScriptRunner;
 
public class Tester
{
    static void Main(
         Microsoft.SharePoint.Administration.SPServiceCollection services1
        ,MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm thisForm
        ,MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser browser
        )
    {        
        
        // Output browser configuration 
        browser.Text = "Raw Display Mode";
        browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Raw;
        browser.ReturnValue = services1;
    }
}
Visual Basic
 
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports MAPILab.SharePoint.Explorer.CodeForm
Imports MAPILab.SharePoint.Explorer.Utilities.ScriptRunner
 
Public Class Tester
    Shared Sub Main(ByVal services1 
    As Microsoft.SharePoint.Administration.SPServiceCollection, 
                    ByVal thisForm 
                    As MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm, 
                    ByVal browser 
                    As MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser)        
        
        ' Output browser configuration 
        browser.Text = "Raw Display Mode" 
        browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Raw
        browser.ReturnValue = services1
    End Sub 
End Class 

As a result of running the script get the following result:

 

Raw Display Mode
Click to open real size screenshot


Now execute the following code, which returns a collection of services in Expanded mode:
C#
 
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using MAPILab.SharePoint.Explorer.CodeForm;
using MAPILab.SharePoint.Explorer.Utilities.ScriptRunner;
 
public class Tester
{
    static void Main(
         Microsoft.SharePoint.Administration.SPServiceCollection services1
        ,MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm thisForm
        ,MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser browser
        )
    {        
        
        // Output browser configuration 
        browser.Text = "Expanded Display Mode";
        browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded;
        browser.ReturnValue = services1;
    }
}
Visual Basic
 
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports MAPILab.SharePoint.Explorer.CodeForm
Imports MAPILab.SharePoint.Explorer.Utilities.ScriptRunner
 
Public Class Tester
    Shared Sub Main(ByVal services1 
    As Microsoft.SharePoint.Administration.SPServiceCollection, 
                    ByVal thisForm 
                    As MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm, 
                    ByVal browser 
                    As MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser)        
        
        ' Output browser configuration 
        browser.Text = "Expanded Display Mode" 
        browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded
        browser.ReturnValue = services1
    End Sub 
End Class 

As a result of running the script get the following result:

 

Expanded Display Mode
Click to open real size screenshot



Example of generating method main when selecting property or field

For instance, several properties or fields are flagged in browser window as shown below.

 

Example


In this case, when creating new script window, method Main will look the following way:
C#
 
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using MAPILab.SharePoint.Explorer.CodeForm;
using MAPILab.SharePoint.Explorer.Utilities.ScriptRunner;
 
public class Tester
{
    static void Main(
         System.Version buildVersion1
        ,bool canRenameOnRestore2
        ,bool canSelectForBackup3
        ,MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm thisForm
        ,MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser browser
        )
    {
                
        // Output browser configuration 
        //browser.Text = "Browser window"; 
        //browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded; 
        
        browser.ReturnValue = null;
    }
}
Visual Basic
 
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports MAPILab.SharePoint.Explorer.CodeForm
Imports MAPILab.SharePoint.Explorer.Utilities.ScriptRunner
 
Public Class Tester
    Shared Sub Main(ByVal buildVersion1 
    As System.Version, 
                    ByVal canRenameOnRestore2 
                    As Boolean, 
                    ByVal canSelectForBackup3 
                    As Boolean, 
                    ByVal thisForm 
                    As MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm, 
                    ByVal browser 
                    As MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser)        
        
        ' Output browser configuration 
        'browser.Text = "Browser window" 
        'browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded 
        
        browser.ReturnValue = Nothing 
    End Sub 
End Class 

As seen in the example, the generated signature of method Main contains, besides obligatory parameter thisForm , automatically added parameters buildVersion1, canRenameOnRestore2, canSelectForBackup3 relevant to objects selected in browser window.

Let`s perform simple modification of generated method. Place string representation of SharePoint version in the header of script window and put string Current SharePoint version is in output box of results:
C#
 
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using MAPILab.SharePoint.Explorer.CodeForm;
using MAPILab.SharePoint.Explorer.Utilities.ScriptRunner;
 
public class Tester
{
    static void Main(
         System.Version buildVersion1
        ,bool canRenameOnRestore2
        ,bool canSelectForBackup3
        ,MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm thisForm
        ,MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser browser
        )
    {   
        // Put SharePoint version to code windows caption 
        thisForm.Text = buildVersion1.ToString();
 
        // Make debug output 
        Debug.Print("Current SharePoint version is {0}", buildVersion1);
        
        // Output browser configuration 
        //browser.Text = "Browser window"; 
        //browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded; 
        
        browser.ReturnValue = null;
    }
}
Visual Basic
 
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports MAPILab.SharePoint.Explorer.CodeForm
Imports MAPILab.SharePoint.Explorer.Utilities.ScriptRunner
 
Public Class Tester
    Shared Sub Main(ByVal buildVersion1 
    As System.Version, 
                    ByVal canRenameOnRestore2 
                    As Boolean, 
                    ByVal canSelectForBackup3 
                    As Boolean, 
                    ByVal thisForm 
                    As MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm, 
                    ByVal browser 
                    As MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser)
        
        'Put SharePoint version to code windows caption 
        thisForm.Text = buildVersion1.ToString()
 
        'Make debug output 
        Debug.Print("Current SharePoint version is {0}", buildVersion1)
        ' Output browser configuration 
        'browser.Text = "Browser window" 
        'browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded 
        
        browser.ReturnValue = Nothing 
    End Sub 
End Class 

In order to be sure of no errors in the programming code, it can be compiled by clicking buttons F6, F7 or selecting menu item Action - Compile.

In order to execute script, click button F5 or select menu item Action - Run.

After executing the given script, window will become as follows (pay attention to points marked with red rectangles):

 

Result
Click to open real size screenshot



Example of generating method main when selecting method

When some method is selected in the browser tree, not only signature of method Main, but also the part of its part is generated - template of call of selected method is put in it beforehand. Let`s demonstrate it.

Assume that one of methods (one for simplicity) is flagged in browser window as shown on the image below (by the way, method selected for the example allows receiving description of SharePoint feature in XML format).

 

Example
Click to open real size screenshot


In this case, when creating new script window, method Main will be the following:
C#
 
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using MAPILab.SharePoint.Explorer.CodeForm;
using MAPILab.SharePoint.Explorer.Utilities.ScriptRunner;
 
public class Tester
{
    static void Main(
         Microsoft.SharePoint.Administration.SPFeatureDefinition mo_getXmlDefinition1
        ,System.Reflection.MethodInfo getXmlDefinition1
        ,MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm thisForm
        ,MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser browser
        )
    {
        // Calling method GetXmlDefinition 
        System.Globalization.CultureInfo value1 = ;
        
        System.Xml.XmlNode value2 = 
            mo_getXmlDefinition1.GetXmlDefinition(
                value1);
                       
        // Output browser configuration 
        //browser.Text = "Browser window"; 
        //browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded; 
        browser.ReturnValue = null;
    }
}
Visual Basic
 
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports MAPILab.SharePoint.Explorer.CodeForm
Imports MAPILab.SharePoint.Explorer.Utilities.ScriptRunner
 
Public Class Tester
    Shared Sub Main(ByVal mo_getXmlDefinition1 
    As Microsoft.SharePoint.Administration.SPFeatureDefinition, 
                    ByVal getXmlDefinition1 
                    As System.Reflection.MethodInfo, 
                    ByVal thisForm 
                    As MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm, 
                    ByVal browser 
                    As MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser)
        
        ' Calling method GetXmlDefinition 
        Dim value1 
        As System.Globalization.CultureInfo = 
        Dim value2 
        As System.Xml.XmlNode = mo_getXmlDefinition1.GetXmlDefinition(value1)
                       
        ' Output browser configuration 
        'browser.Text = "Browser window" 
        'browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded 
        
        browser.ReturnValue = Nothing 
    End Sub 
End Class 

As seen in the example, besides object of type MethodInfo selected in the browser, the list of parameters of method Main includes also parameter mo_getXmlDefinition1 containing reference to an object that must be called. Moreover, method body already contains template that can simplify the call of selected method. It remains only to supply it (actually to define value of variable value1) and use the result of call that will be placed in variable value2. Modify the code for xml describing property of SharePoint feature to be shown in pop-up window, and after that - object of type XmlNode to appear in new browser window:
C#
 
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using MAPILab.SharePoint.Explorer.CodeForm;
using MAPILab.SharePoint.Explorer.Utilities.ScriptRunner;
 
public class Tester
{
    static void Main(
         Microsoft.SharePoint.Administration.SPFeatureDefinition mo_getXmlDefinition1
        ,System.Reflection.MethodInfo getXmlDefinition1
        ,MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm thisForm
        ,MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser browser
        )
    {
        // Calling method GetXmlDefinition 
        System.Globalization.CultureInfo value1 = System.Threading.Thread.CurrentThread.CurrentCulture;
        
        System.Xml.XmlNode value2 = 
            mo_getXmlDefinition1.GetXmlDefinition(
                value1);
        
        // Show Feature XML using MessageBox class 
        System.Windows.Forms.MessageBox.Show(value2.OuterXml);
                              
        // Output browser configuration 
        //browser.Text = "Browser window"; 
        //browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded; 
        
        // Return results to the new object browser 
        browser.ReturnValue = value2;
    }
}
Visual Basic
 
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports MAPILab.SharePoint.Explorer.CodeForm
Imports MAPILab.SharePoint.Explorer.Utilities.ScriptRunner
 
Public Class Tester
    Shared Sub Main(ByVal mo_getXmlDefinition1 
    As Microsoft.SharePoint.Administration.SPFeatureDefinition, 
                    ByVal getXmlDefinition1 
                    As System.Reflection.MethodInfo, 
                    ByVal thisForm 
                    As MAPILab.SharePoint.Explorer.CodeForm.MLCodeForm, 
                    ByVal browser 
                    As MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.MLBrowser)
        
        ' Calling method GetXmlDefinition 
        Dim value1 
        As System.Globalization.CultureInfo
        value1 = System.Threading.Thread.CurrentThread.CurrentCulture
        
        Dim value2 
        As System.Xml.XmlNode
        value2 = mo_getXmlDefinition1.GetXmlDefinition(value1)
             
        'Show Feature XML using MessageBox class 
        System.Windows.Forms.MessageBox.Show(value2.OuterXml)
                       
        ' Output browser configuration 
        'browser.Text = "Browser window" 
        'browser.DisplayMode = 
        MAPILab.SharePoint.Explorer.Utilities.ScriptRunner.DisplayMode.Expanded 
        
        'Return results to the new object browser 
        browser.ReturnValue = value2
    End Sub 
End Class 

As a result of script execution (button F5) we will sequentially see:

 

Result
Click to open real size screenshot

 

Result
Click to open real size screenshot


Here we have reviewed examples of operations with properties or methods separately, however nothing prevents from selecting both of them in the browser simultaneously. In this case, signature of method Main, which includes both first and second variant, will be generated.

Our valued customers

HarePoint products are recognized by thousands of companies

  • Intel
  • Bombardier Aerospace
  • WilmerHale
  • Wargaming Interactive
  • Bre bank sa