Blogger

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

    Recent items

Login

Luca Costante - Items filtered by date: October 2014

I would to share with the network the snippet code to enable the Scheduling settings via Client SharePoint Object Model APIs.

This first block provide to enable the Scheduling on all pages list of the site collection

Show/Hidden csharp code

View source
 
 
 
 public void EnablePagesSchedulingOnAllSiteCollection(CustomWebApplication wa)
        {
            try
            {
                foreach (CustomSiteCollection sc in wa.SiteCollections)
                {
                    using (ClientContext context = new ClientContext(wa.Url + sc.Url))
                    {
                        context.Credentials = new NetworkCredential(wa.UserName, wa.UserPassword, wa.UserDomain);
                        context.Load(context.Site.RootWeb, w => w.Webs, w => w.ServerRelativeUrl);
                        context.ExecuteQuery();
 
                        PageHelper.EnableScheduling(context, context.Site.RootWeb, true,sc.LanguageShortCode,logFilePath);
 
 
                    }
                }
            }
            catch (Exception ex)
            {
                string message = string.Format("------------- Exception EnablePagesSchedulingOnAllSiteCollection: {0}", ex.Message);
                Logger.WriteLog(message, logFilePath);
            }
        }
 
 
 

 

The second block provide to enable the settings on the list page recursively

Show/Hidden csharp code

View source
 
 
 
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Publishing;
using Microsoft.SharePoint.Publishing.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Client.Common.Helpers
{
    public class PageHelper
    {
 
        public static Guid StartDateGUID
        {
            get
            {
                return new Guid("51d39414-03dc-4bd0-b777-d3e20cb350f7");
            }
        }
 
        public static Guid EndDateGUID
        {
            get
            {
                return new Guid("a990e64f-faa3-49c1-aafa-885fda79de62");
            }
        }
 
        internal static string GetPageListName(string language)
        {
            string pagesListName = string.Empty;
            switch (language)
            {
                case "it": pagesListName = "Pagine"; break;
                default: pagesListName = "Pages"; break;
            }
            return pagesListName;
        }
 
        internal static void EnableScheduling(ClientContext context, Web currWeb, bool recursive, string language, string logFilePath)
        {
            try
            {
                List pages = ListHelper.GetList(context, currWeb, GetPageListName(language), logFilePath);
                EventReceiverDefinitionCollection eventReceivers = pages.EventReceivers;
 
 
                pages.EnableModeration = true;
                pages.EnableMinorVersions = true;
 
                pages.Update();
 
 
                //To enable Scheduling in the list
                string assembly = "Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c";
                string fullName = "Microsoft.SharePoint.Publishing.Internal.ScheduledItemEventReceiver";
 
                EventReceiverType typeItemUpdating = EventReceiverType.ItemUpdating;
                EventReceiverType typeItemAdded = EventReceiverType.ItemAdded;
                string itemUpdatingERName = "Item Updating Event Handler For Scheduling";
                string itemAddedERName = "Item Added Event Handler For Scheduling";
 
                bool foundUpdating = false;
                bool foundAddedd = false;
 
                foreach (EventReceiverDefinition e in eventReceivers)
                {
                    if (string.Compare(e.ReceiverName, itemUpdatingERName, true) == 0)
                        foundUpdating = true;
                    if (string.Compare(e.ReceiverName, itemAddedERName, true) == 0)
                        foundAddedd = true;
                }
 
                if (!foundUpdating)
                {
 
                    EventReceiverDefinitionCreationInformation erItemUpdating = new EventReceiverDefinitionCreationInformation();
                    erItemUpdating.ReceiverName = itemUpdatingERName;
                    erItemUpdating.EventType = typeItemUpdating;
                    erItemUpdating.ReceiverAssembly = assembly;
                    erItemUpdating.ReceiverClass = fullName;
 
                    eventReceivers.Add(erItemUpdating);
                }
 
                if (!foundAddedd)
                {
                    EventReceiverDefinitionCreationInformation erItemAdded = new EventReceiverDefinitionCreationInformation();
                    erItemAdded.ReceiverName = itemAddedERName;
                    erItemAdded.EventType = typeItemAdded;
                    erItemAdded.ReceiverAssembly = assembly;
                    erItemAdded.ReceiverClass = fullName;
 
                    eventReceivers.Add(erItemAdded);
                }
 
                List<FieldLinkCreationInformation> fieldsToAdd = new List<FieldLinkCreationInformation>();
                fieldsToAdd.Add(new FieldLinkCreationInformation() { Field = context.Site.RootWeb.Fields.GetById(StartDateGUID) });
                fieldsToAdd.Add(new FieldLinkCreationInformation() { Field = context.Site.RootWeb.Fields.GetById(EndDateGUID) });
 
                context.Load(fieldsToAdd[0].Field, f => f.Id);
                context.Load(fieldsToAdd[1].Field, f => f.Id);
 
                context.ExecuteQuery();
 
 
                foreach (FieldLinkCreationInformation f in fieldsToAdd)
                {
                    if (pages.Fields.Where(c => c.Id == f.Field.Id).Count() == 0)
                        pages.Fields.Add(f.Field);
                    else
                    {
                        pages.Fields.Where(c => c.Id == f.Field.Id).FirstOrDefault().Hidden = false;
                        pages.Fields.Where(c => c.Id == f.Field.Id).FirstOrDefault().Update();
                    }
                }
 
                context.ExecuteQuery();
 
                foreach (ContentType ct in pages.ContentTypes)
                {
                    try
                    {
                        context.Load(ct, c => c.FieldLinks, c => c.Fields, c => c.Name);
                        context.ExecuteQuery();
 
                        foreach (FieldLinkCreationInformation f in fieldsToAdd)
                        {
 
                            if (ct.FieldLinks.Where(c => c.Id == f.Field.Id).Count() == 0)
                                ct.FieldLinks.Add(f);
                            else
                                ct.FieldLinks.Where(c => c.Id == f.Field.Id).FirstOrDefault().Hidden = false;
 
                            if (ct.Fields.Where(c => c.Id == f.Field.Id).Count() == 0)
                                ct.Fields.Add(f.Field);
                            else
                                ct.Fields.Where(c => c.Id == f.Field.Id).FirstOrDefault().Hidden = false;
                        }
                        ct.Update(true);
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteLog(string.Format("Error on CT {0}: {1} ", ct.Name, ex.Message), logFilePath);
                    }
                }
 
 
                if (!pages.DefaultView.ViewFields.Contains("Scheduling Start Date"))
                {
                    pages.DefaultView.ViewFields.Add("Scheduling Start Date");
                }
 
                if (!pages.DefaultView.ViewFields.Contains("Scheduling End Date"))
                {
                    pages.DefaultView.ViewFields.Add("Scheduling End Date");
                }
 
                pages.DefaultView.Update();
                pages.Update();
                context.ExecuteQuery();
 
                Logger.WriteLog(string.Format("Publishing enabled on list Pages of site {0}.", currWeb.ServerRelativeUrl), logFilePath);
 
                if (recursive)
                {
                    context.Load(currWeb, w => w.Webs);
                    context.ExecuteQuery();
 
                    foreach (Web subWeb in currWeb.Webs)
                    {
                        EnableScheduling(context, subWeb, recursive, language, logFilePath);
                    }
                }
 
            }
            catch (Exception ex)
            {
                Logger.WriteLog(string.Format("Error during EnableScheduling on web {0}: {1}", currWeb.ServerRelativeUrl, ex), logFilePath);
            }
        }
    }
}
 

Published in SharePoint

Today I'm working with a particular error: when I search items that contains the single quote character through the REST API, I receive a bad request error (400 error).


The first steps is to analyze the response from the server:

  • Request: GET http://test.lucacostante.com/_api/search/query?querytext='path:http://test.lucacostante.com/Lists/Banners/Forms/AllItems.aspx OR path:http://test.lucacostante.com/Pages/L'universo.aspx'&rowlimit=5&selectproperties='Title,Path'
  • Response: Error 400: {"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"The expression \"'path:http://test.lucacostante.com/Lists/Banners/Forms/AllItems.aspx OR path:http://test.lucacostante.com/Pages/L'universo.aspx'\" is not valid."}}}

The problem is the single quote ' character into the query that provide to create a wrong query.

I've tried to encode the URL but this not resolve my problem. The solution is simpler: I need only to add another single quote to resolve my problem ('').

  • Request: GET http://test.lucacostante.com/_api/search/query?querytext='path:http://test.lucacostante.com/Lists/Banners/Forms/AllItems.aspx OR path:http://test.lucacostante.com/Pages/L''universo.aspx'&rowlimit=5&selectproperties='Title,Path'
  • Response: HTTP/1.1 200 OK
Published in SharePoint