HarePoint Explorer for SharePoint - Building scripts
 

Building scripts

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

When opening window of script creation, HarePoint Explorer for SharePoint generates its 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 the descriptiThis code contains the description of the Tester class, which includes statistical Main method.

The Main method is a start point for executing the script. Signature of this method is generated automatically on the basis of selected objects in browser windows. Disregard of the condition of the browser windows, the main method always gets parameter thisForm and returns the object of the object type. The thisForm parameter contains the reference to the current script building window. Object returned by the Main method is displayed in the new browser window (except for cases when null reference is returned).

Only name of the Tester class and the signature of the Main method should remain unchanged in the script programming code – everything else can be changed as you like. You can also add descriptions of additional types, modify description of both the Tester class and the body of the Main method, etc.

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

Example of use the browser parameter and its parameters

The browser parameter is used to visualize and return one or more if necessary facilities during the execution of the script. Parameter browser has the following properties:

  1. Text - Heading tab of object browser window. Displays the name of the object or its type if the property is null, .
  2. DisplayMode - The object appearance 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 mode.
  3. RaturnValue - Object to display in the object browser window.

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

Mark the property Services in the browser window.

Show screenshot

Let the browser window is marked property Services.

Create a new script window and execute the following code, which returns a collection of services in a 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:

Show screenshot

Collection of Services in Raw Display Mode

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:

Show screenshot

Collection of Services in Raw Display Mode

Example of generating method Main when selecting property or field

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

Show screenshot

For instance, several properties or fields are flagged in browser window

In this case, when creating new script window, the Main method 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 you can see in the example, the generated signature of the Main method contains, together with thisForm obligatory parameter, automatically added buildVersion1, canRenameOnRestore2, canSelectForBackup3 parameters, relevant to the objects selected in the browser window.

Let`s perform simple modification of generated method. Place string representation of the SharePoint version in the header of the script window and put the Current SharePoint version is … string into 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):

Show screenshot

Example of generating method Main when selecting method

When some method is selected in the browser tree, exept the signature of the Main method the part of its body is generated – template of call of selected method is put in it beforehand. Let`s demonstrate it.

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

Show screenshot

In this case, on creating new script window, the Main method 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 the Main method 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:

Show screenshot

Show 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 the Main method , which includes both first and second variant, will be generated.