Wednesday, 28 May 2014

Sharepoint - Object # has no method 'showModalDialog' in chrome

 Consider the following code:
function ShowWelcomeDialog() {
    var data = {};

    var options = {
        url: "/_layouts/xxxx/xxxxxss.aspx",
        title: "xxx xxx",
        width: 700,
        height: 950,
        dialogReturnValueCallback: myDialogCallback,
        args: JSON.stringify(data)
    };

    SP.UI.ModalDialog.showModalDialog(options);
    return false;
}
 I have this code for a button click and the modal opens fine in firefox and ie. But it doesn't open in Chrome. It gives the following error in console:
Uncaught TypeError:Object # has no method 'showModalDialog'..
 Solution:

Replace the line:
SP.UI.ModalDialog.showModalDialog(options);
with
SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);



StackOverFlow

Tuesday, 27 May 2014

Sharepoint- Using an Image From formatmap32x32.png in a Ribbon Control

As you know in SharePoint , ribbon button images are coming from a built in image called formatmap32x32.png and formatmap16x16.png which are located at:
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\1033\IMAGES
So if you are creating custom ribbon buttons, and if you want to use one of the built in button image, then how would you set Image32by32Top and Image32by32Left ?
Here is the answer.
Get the image from from the above path. Choose the icon image which you would like yo use.
As shown below, say the icon which chose is a pie chart as marked with black outline.
Count its position from top. its 7. also count its position from left, its 8. 
Since the image is 32*32, you would calculate as left=32*8= 256 and Top=224.

Now in your Elements.xml use this counts as:
Image32by32="/_layouts/$Resources:core,Language;/images/formatmap32x32.png" Image32by32Top="-224" Image32by32Left="-256"
Use the similar steps for  formatmap16x16.png, only the difference is that multiply your count by 16, not 32.

Monday, 26 May 2014

Programmatically logging to the Sharepoint ULS

Logging helps one to quickly tracks bugs and evaluate the health of the system under development.
SharePoint 2010 allows to log into ULS for our custom code components.
Logging in SharePoint 2010 ULS is quite easy. Using just a few lines of code, you can log info from your custom components:
[StackOverflow answer ]

     try
        {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
                diagSvc.WriteTrace(123456, new SPDiagnosticsCategory("Category_Name_Here",
TraceSeverity.Monitorable, EventSeverity.Error), TraceSeverity.Monitorable,"{0}:{1}", new object[] { "Method_Name", "Error_Message"});
            });
        }
        catch (Exception ex)
        {
        }


Put above code in some method, and whenever you want to log, just call that method by passing suitable parameters.


If you would like to log with your product name, you can create a custom logger class:
using System.Collections.Generic;
using Microsoft.SharePoint.Administration;

namespace Workflow.Common
{
    public class LoggingService : SPDiagnosticsServiceBase
    {
        public static string DiagnosticAreaName = "Workflow";
        private static LoggingService _Current;
        public static LoggingService Current
        {
            get
            {
                if (_Current == null)
                {
                    _Current = new LoggingService();
                }

                return _Current;
            }
        }

        private LoggingService()
            : base("Workflow Logging Service", SPFarm.Local)
        {

        }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
            {
                new SPDiagnosticsArea(DiagnosticAreaName, new List<SPDiagnosticsCategory>
                {
                    new SPDiagnosticsCategory("Workflow Info", TraceSeverity.Monitorable, EventSeverity.Information),
                    new SPDiagnosticsCategory("Workflow Error", TraceSeverity.Unexpected, EventSeverity.Error)
                })
            };

            return areas;
        }

        public static void LogError(string projectName, string errorMessage, object[] data)
        {
            SPDiagnosticsCategory category = LoggingService.Current.Areas[DiagnosticAreaName].Categories["Workflow Error"];
            LoggingService.Current.WriteTrace(123456, category, TraceSeverity.Unexpected, projectName + " - " + errorMessage, data);
        }
        public static void LogError(string projectName, string errorMessage)
        {
            SPDiagnosticsCategory category = LoggingService.Current.Areas[DiagnosticAreaName].Categories["Workflow Error"];
            LoggingService.Current.WriteTrace(123456, category, TraceSeverity.Unexpected, projectName + " - " + errorMessage);
        }
        public static void LogInformation(string projectName, string errorMessage, object[] data)
        {
            SPDiagnosticsCategory category = LoggingService.Current.Areas[DiagnosticAreaName].Categories["Workflow Info"];
            LoggingService.Current.WriteTrace(123456, category, TraceSeverity.Monitorable, projectName + " - " + errorMessage, data);
        }
        public static void LogInformation(string projectName, string errorMessage)
        {
            SPDiagnosticsCategory category = LoggingService.Current.Areas[DiagnosticAreaName].Categories["Workflow Info"];
            LoggingService.Current.WriteTrace(123456, category, TraceSeverity.Monitorable, projectName + " - " + errorMessage);
        }
    }
}
 Then you can access this class to log your custom information:
try
{
    //your code
}
catch (Exception ex)
{
    LoggingService.LogError("Workflow", ex.Message);
}