Export (0) Print
Expand All

How to: Use Conditional Scope

SharePoint 2010

Last modified: August 22, 2011

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 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);
            }
        }
    }
}
Show:
© 2014 Microsoft