Sunday, September 23, 2012

Select custom ribbon tab - jQuery

    I created custom ribbon tab and a button on a page. When  a user press the button on the page, I needed to focus him to the custom ribbon tab. Of course, I used 
_ribbonStartInit("Ribbon.CustomTab", false, null);

but the problem is, after a user changes it to another tab, i.e. "Read" or "Browse" the second pressing doesn't work. Therefore, the solution was - adding SelectRibbonTab() function:

$('img.CustomTabButtonClass').bind('click', function() { 
    
  if (typeof(_ribbonStartInit) == "function")
   {
     try 
     {
       _ribbonStartInit("Ribbon.CustomTab", false, null);
     }
     catch(e)
       {
                  
        }
     SelectRibbonTab("Ribbon.CustomTab", true);
    }
    });

Tuesday, September 11, 2012

CSS: Cursor hand not working in chrome, firefox

The problem is  - cursor "hand" is not a supported style in browsers other than IE.
Instead of it need to use :
cursor: "pointer"

The vertical scrolling bar doesn't work in Chrome browser

Like most of us, I faced this problem when scroll bar doesn't work in Chrome.
After googling, the solution was a jQuery script which I inserted to the master page:



Sunday, August 12, 2012

Create List Definition for Picture Library - C#


If you want to do it in Visual Studio, you're unable to see this definition in the drop down list. However it is very easy to do it:


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

namespace Console
{
    class Program
    {
        string listName = "Picture Library";
        string description = "My  Picture Library "; 

        public static void Main(string[] args)
        {
            using (SPSite oSite = new SPSite("http://contoso.com"))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    if (oWeb.Lists.TryGetList(listName) != null)
                    {
                        // SPListTemplate class - Used to represent the list definition or a list template 
                        // oWeb.ListTemplates - Used to get the "Picture Library" list template
      
                        SPListTemplate oTemplate = oWeb.ListTemplates["PictureLibrary"];

                        //Creates a new list with Title, Descrription and List Template "Picture Library" type

                        oWeb.Lists.Add(listName, description, oTemplate);
                    }
                    else
                    {
                        System.Console.WriteLine(listName + " already exists in the " + oWeb.Title + " site");
                        System.Console.ReadLine();
                    }
                }
            }
        }
    }
}

Wednesday, August 8, 2012

Add Title to Social Comments - jQuery

To insert some title or another text to social comments on a page:
You can resolve it by jQuery code:


  $(document).ready(function () {

            var socomHolders = $('div [id*="_socomHolder_"]');
            if (socomHolders.length) {

                socomHolders.each(function () {

                    var phName = $(this).attr('id');
                    var index = phName.substring(phName.lastIndexOf('socomHolder') + 
                        12, phName.length);

                    var socomContent = $('div [id*="_socomContents_' + index +'"]');
                    var newRow = $("Example : "+ index +"");
                    $(".ms-socialCommentItem", socomContent).prepend(newRow);
                });
            }
        });


Tuesday, July 31, 2012

How to know if the Page is in Edit Mode - C#

For a page we need to use:
if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Display)
  {
   //  display mode
  }
  else // Microsoft.SharePoint.SPContext.Current.FormContext.FormMode = SPControlMode.Edit
  {
   //  edit mode
  }



and for webpart pages:

   WebPartManager mgr = this.WebPartManager;
 if (mgr.DisplayMode == WebPartManager.EditDisplayMode)
     {
        // edit mode
     }
  else
     {
  //  display mode
     }

How to know if the Page is in Edit Mode - JavaScript ?

For a page we need to use:

var pageMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;

if (pageMode == 1)
{
// page is in edit mode
}
else
{
// page is in browse mode
}



and for wiki pages:



var pageMode = document.forms[MSOWebPartPageFormName]._wikiPageMode.value;

if (pageMode == "Edit")
{
// page is in edit mode
}
else
{
// page is in browse mode
}



This will refer to a value of the following html input control, which is rendering on the page when it is in edit mode: