Compartir a través de


How to: Execute a SharePoint command

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

If you want to use the server object model in a SharePoint tools extension, you must create a custom SharePoint command to call the API. After you define the command and deploy it with your SharePoint tools extension, your extension can execute the command to call into the SharePoint server object model. To execute the command, use one of the ExecuteCommand methods of an ISharePointConnection object.

For more information about the purpose of SharePoint commands, see Call into the SharePoint object models.

To execute a SharePoint command

  1. In your SharePoint tools extension, get an ISharePointConnection object. The way you get an ISharePointConnection object depends on the type of extension you are creating:

  2. Call one of the ExecuteCommand methods of the ISharePointConnection object. Pass the name of the command you want to execute to the first argument of the ExecuteCommand method. If your command has a custom parameter, pass that parameter to the second argument of the ExecuteCommand method.

    There is a different ExecuteCommand overload for each supported command signature. The following table lists the supported signatures and which overload to use for each signature.

    Command signature ExecuteCommand overload to use
    The command has only the default ISharePointCommandContext parameter and no return value. ExecuteCommand
    The command has only the default ISharePointCommandContext parameter and a return value. ExecuteCommand
    The command has two parameters (the default ISharePointCommandContext parameter and a custom parameter) and no return value. ExecuteCommand
    The command has two parameters and a return value. ExecuteCommand

Example

The following code example demonstrates how to use the ExecuteCommand overload to call the Contoso.Commands.UpgradeSolution command that is described in How to: Create a SharePoint command.

public void Execute(IDeploymentContext context)
{
    context.Logger.WriteLine("Upgrading solution: " + solutionName, LogCategory.Status);
    context.Project.SharePointConnection.ExecuteCommand("Contoso.Commands.UpgradeSolution",
        solutionFullPath);
}
Private Sub Execute(ByVal context As IDeploymentContext) _
    Implements IDeploymentStep.Execute
    context.Logger.WriteLine("Upgrading solution: " & solutionName, LogCategory.Status)
    context.Project.SharePointConnection.ExecuteCommand("Contoso.Commands.UpgradeSolution", _
        solutionFullPath)
End Sub

The Execute method shown in this example is an implementation of the Execute method of the IDeploymentStep interface in a custom deployment step. To see this code in the context of a larger example, see Walkthrough: Create a custom deployment step for SharePoint projects.

Note the following details about the call to the ExecuteCommand method:

  • The first parameter identifies the command that you want to call. This string matches the value that you pass to the SharePointCommandAttribute on the command definition.

  • The second parameter is the value that you want to pass to the custom second parameter of the command. In this case, it is the full path of the .wsp file that is being upgraded to the SharePoint site.

  • The code does not pass the implicit ISharePointCommandContext parameter to the command. This parameter is passed into the command automatically when you call the command from an extension of the SharePoint project system or an extension of the SharePoint Connections node in Server Explorer. In other types of solutions, such as in a project template wizard that implements the IWizard interface, this parameter is null.

Compile the code

This example requires a reference to the Microsoft.VisualStudio.SharePoint assembly.

See also