Tuesday, 2 July 2013

Programmatically Establish the connection between existing List View Webparts on page

        /// <summary>
        /// This method is used to establish a connection between  two XsltListViewWebParts in the same page
        /// </summary>
        /// <param name="web"></param>
        /// <param name="pagename"></param>
        /// <returns></returns>
        private void ConnectableWebParts(SPWeb web, string pagename)
        {
            try
            {
                // Get the WebParts that need to get connected
                string providerWebPartTitle = "PROVIDER WEBPART TITLE";
                string consumerWebPartTitle = "CONSUMER WEBPART TITLE";
                //ListViewWp(web);
                string url = web.Url.Trim('/');

                // get the web part manage which we will use to interact
                SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager(url + "/SitePages/" + pagename, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
                XsltListViewWebPart providerPart = null;
                XsltListViewWebPart consumerPart = null;

                foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpManager.WebParts)
                {
                    if (wp.Title.Equals(providerWebPartTitle, StringComparison.CurrentCultureIgnoreCase))
                    {
                        providerPart = wp as XsltListViewWebPart;
                    }
                    else if (wp.Title.Equals(consumerWebPartTitle, StringComparison.CurrentCultureIgnoreCase))
                    {
                        consumerPart = wp as XsltListViewWebPart;
                    }
                }

                // get connectionpoints
                ProviderConnectionPointCollection providerConnections = wpManager.GetProviderConnectionPoints(providerPart);
                ProviderConnectionPoint pCP = null;
                if (providerConnections != null)
                {
                    pCP = providerConnections["DFWP Row Provider ID"];
                }

                ConsumerConnectionPointCollection consumerConnections = wpManager.GetConsumerConnectionPoints(consumerPart);
                ConsumerConnectionPoint cCP = null;
                if (consumerConnections != null)
                {
                    cCP = consumerConnections["DFWP Filter Consumer ID"];
                }

                SPRowToParametersTransformer transformer = new SPRowToParametersTransformer();
                transformer.ProviderFieldNames = new string[] { "@Title" };
                transformer.ConsumerFieldNames = new string[] { "Consumer Field Name(i.e Lookup column name)" };

                // connect the webparts
                SPWebPartConnection filterConn = wpManager.SPConnectWebParts(providerPart, pCP, consumerPart, cCP, transformer);
                wpManager.SPWebPartConnections.Add(filterConn);

            }
            catch (Exception ex)
            {
            
            }
        }

Thursday, 16 May 2013

Create new Custom List with existing List Definition\Template



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

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
           
            using (SPSite site = new SPSite("http://xxxx/"))
            {
               using (SPWeb web = site.OpenWeb())
                {
                    SetupLists(web);
                }
            }
        }

        /// <summary>
        /// This method is used to setup list
        /// </summary>
        /// <param name="web"></param>
        private static void SetupLists(SPWeb web)
        {
            using (var newWeb = web.Site.OpenWeb(web.ID))
            {
                SetupCustomLists(newWeb);
                newWeb.Update();
            }
        }

        /// <summary>
        /// This method is used to setup custom list by providing Custom List Template Name
        /// </summary>
        /// <param name="web"></param>
        private static void SetupCustomLists(SPWeb web)
        {
            var customListTemplate = GetListTemplate(web, " customListTemplateName");
            SetupList(web, "CustomList", "A new custom list.", customListTemplate);

            // ... any other lists of this type that need created ...
        }

        /// <summary>
        /// This method is used to get ListTemplate with given name as parameter
        /// </summary>
        /// <param name="web"></param>
        /// <param name="templateName"></param>
        /// <returns></returns>
        private static SPListTemplate GetListTemplate(SPWeb web, string templateName)
        {
            var template = web.ListTemplates.OfType<SPListTemplate>().FirstOrDefault(i => i.Name == templateName);
            if (template == null)
                throw new SPException(string.Format("Template {0} not found.", templateName));
            return template;
        }

        /// <summary>
        /// This method is used to creates/setups the new custom list with existing template
        /// </summary>
        /// <param name="web"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="template"></param>
        private static void SetupList(SPWeb web, string name, string description, SPListTemplate template)
        {
            var webId = web.ID;
            SPList list;
            using (var newWeb = web.Site.OpenWeb(webId))
            {
                list = EnsureList(newWeb, name, description, template);
            }

            // ... perform any additional actions needed on list ...
        }

        /// <summary>
        /// This method ensures the newly created cusomt list
        /// </summary>
        /// <param name="web"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        private static SPList EnsureList(SPWeb web, string name, string description, SPListTemplate template)
        {
            var list = web.Lists.TryGetList(name);
            if (list != null)
            {
                if (list.BaseTemplate != template.Type)
                    throw new SPException(string.Format("List {0} has type '{1}'; but should have type '{2}'.", name, list.BaseTemplate, template.Type));
                return list;
            }

            var id = web.Lists.Add(name, description, template);
            return web.Lists[id];
        }
    }

}

Wednesday, 12 December 2012

Does SharePoint 2010 solution work on SharePoint 2013?





Answer : The solution gets deployed to the 14 hive, because of a setting in the manifest.xml file in the page. If you open up the package in Visual Studio, or you can open the manifest file directly to change this.
If you created your solution in SharePoint 2010 the <Solution> element in the manifest file will have a property that says "SharePointProductVersion="14.0". If you change this to 15.0 the solution should deploy to the 15 hive.