Friday, August 10, 2012

SharePoint: Visual Web Part(Any Web Part) with CSOM code on Sandbox Solution.

If you are initializing Web or Site object with URL and target deployment type is Sandbox, You will get below error.

Web Part Error: Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred.

If you want to resolve this issue, change deployment type from Sandbox to Farm.




Tuesday, July 31, 2012

SharePoint: Add Navigation Node using CSOM

Client side object model can update navigation node remotely.  Below is code to add element to top navigation.

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

namespace NavigationCustomAction
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext context = new ClientContext("http://goazrapp18:33369");
            Web web = context.Web;
            AddNavigationItem(context, web);


            Console.WriteLine("Please press any key to exit");
            Console.ReadLine();
        }

        private static void AddNavigationItem(ClientContext context, Web web)
        {
            NavigationNodeCreationInformation navigationNodeCreationInformation = new NavigationNodeCreationInformation();
            navigationNodeCreationInformation.Title = "Custom Node";
            navigationNodeCreationInformation.Url = "http://gooogle.com";
            navigationNodeCreationInformation.IsExternal = true;
            navigationNodeCreationInformation.AsLastNode = true;
            web.Navigation.TopNavigationBar.Add(navigationNodeCreationInformation);
            web.Update();
            context.ExecuteQuery();
            Console.WriteLine("Node created Successfully");
        }
    }
}


If you want to add to Quick Launch bar, you have to update one line as below.

web.Navigation.QuickLaunch.Add(navigationNodeCreationInformation);

This will add element at Quick Launch bar as well.