How to: Use Conditional Scope

Applies to: SharePoint Foundation 2010

Available in SharePoint Online

To execute code conditionally, use the ConditionalScope class to set conditions. If the conditions are met, the StartScope() method of this class specifies which action to perform, and its TestResult property contains the result of testing the conditions.

The following example retrieves and displays the title of a specific list template type for a site collection, as long as the list exists and the current user has permissions to manage lists.

Note

The ECMAScript (JavaScript, JScript) object model does not include an equivalent for the ConditionalScope class in the .NET managed and Silverlight object models.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
            Web oWebsite = clientContext.Web;
            List oList = oWebsite.GetCatalog((int)ListTemplateType.WebPartCatalog);

            BasePermissions permissions = new BasePermissions();
            permissions.Set(PermissionKind.ManageLists);

            ConditionalScope scope = new ConditionalScope(
                clientContext,
                () => oList.ServerObjectIsNull.Value != true
                    && oWebsite.DoesUserHavePermissions(permissions).Value == true);

            using (scope.StartScope())
            {
                clientContext.Load(oList,
                    list=>list.Title);
            }

            clientContext.ExecuteQuery();

            Console.WriteLine(scope.TestResult.Value);

            if (scope.TestResult.Value)
            {
                Console.WriteLine(oList.Title);
            }
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Microsoft.SharePoint.Client
Imports SP = Microsoft.SharePoint.Client

Namespace Microsoft.SDK.SharePointServices.Samples
    Class Program

        Public Shared Sub Main()

            Dim clientContext As New ClientContext("http://MyServer/sites/MySiteCollection")
            Dim oWebsite As Web = clientContext.Web
            Dim oList As List = oWebsite.GetCatalog(ListTemplateType.WebPartCatalog)

            Dim permissions As New BasePermissions()
            permissions.Set(PermissionKind.ManageLists)

            Dim scope As New ConditionalScope(clientContext, _
                Function() oList.ServerObjectIsNull.Value <> True _
                    AndAlso oWebsite.DoesUserHavePermissions(permissions).Value = True)

            Using scope.StartScope()
                clientContext.Load(oList, Function(list) list.Title)
            End Using

            clientContext.ExecuteQuery()

            Console.WriteLine(scope.TestResult.Value)

            If scope.TestResult.Value Then
                Console.WriteLine(oList.Title)
            End If
        End Sub
    End Class
End Namespace

See Also

Concepts

How to: Use Exception Handling Scope

Common Programming Tasks in the Managed Client Object Model

Other Resources

Client Class Library