Monday, April 30, 2012

Helpers in Sharepoint Code :

Last week I managed to secure decent score in getting my MCTS Sharepoint configuration exam.
Being a hectic week ( I had a >100% utilization as well at work), I wanted to share some good code snippets with everyone which helps us get things done faster in Sharepoint ...I am panning to share with everyone my preperation guide and my strategy to get going in a week's timeframe if someone's interested in getting certified!

Now..coming back to the blog...
Helper functions to the rescue: Often as developers we use helper functions, I have planned to use this space to collate them here to get all of them at one Central location!

  • Get Custom User profile properties
Often we create custom properties in Active directory which need to be accessible via code. In my most recent project we had such a scenario and a generic helper function below helped us in code reuse

accountName can be the account of the current logged in user  or any user and property is the string which we need to find.
   // this function fetches a property of a user
        public static string GetUserProperty(SPSite site, string accountName, string property)
        {
            try
            {
                string userProperty = string.Empty;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    var UserProfMgr = new UserProfileManager(context);
                    UserProfile UserProf = UserProfMgr.GetUserProfile(accountName);

                    if (UserProf[property].Value != null)
                    {
                        userProperty = UserProf[property].Value.ToString();
                    }
                });
                return userProperty;
            }

            catch (Exception ex)
            {
                throw new Exception("Could not get the property:" + property + "of the User:" + accountName);
            }

        }
    }
  • Trace Log an Exception
Often for the development team it is a challenge to debug issues in share point. Catching and Throwing proper exceptions is the Best way to troubleshoot issues. the one class i have started using recently is below :

    Define an enum for the severity :

   public enum LogSeverity
    {
        Unexpected,
        High,
        Medium,
        Info,
        Monitorable,
        Verbose,
        VerboseEx,
        None
    }


Class file :
 public static class TraceLog
    {
        private static DiagnosticsService myULS = null;

        public static void Log(string msg, LogSeverity severity)
        {
            if (myULS == null)
            {
                myULS = DiagnosticsService.Local;
            }

            if (myULS != null)
            {
                TraceSeverity tSeverity = mapTraceSeverity(severity);
                SPDiagnosticsCategory cat = myULS[CategoryId.General];

                myULS.WriteTrace(5555, cat, tSeverity, msg, myULS.TypeName);
            }
        }

        public enum CategoryId
        {
            None = 0,
            Deployment = 100,
            Provisioning = 200,
            CustomAction = 300,
            Rendering = 400,
            WebPart = 500,
            General = 600
        }

        [System.Runtime.InteropServices.GuidAttribute("DBEWW5AB-C5A7-46B5-A2BB-5581F960C333")]
        class DiagnosticsService : SPDiagnosticsServiceBase
        {
            private static string DiagnosticsAreaName = "Project Name";

            public DiagnosticsService()
            {
            }

            public DiagnosticsService(string name, SPFarm farm)
                : base(name, farm)
            {
            }

            protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
            {
                List<SPDiagnosticsCategory> categories = new List<SPDiagnosticsCategory>();
                foreach (string catName in Enum.GetNames(typeof(CategoryId)))
                {
                    uint catId = (uint)(int)Enum.Parse(typeof(CategoryId), catName);
                    categories.Add(new SPDiagnosticsCategory(catName, TraceSeverity.Verbose, EventSeverity.Error, 0, catId));
                }

                yield return new SPDiagnosticsArea(DiagnosticsAreaName, categories);
            }

            public static DiagnosticsService Local
            {
                get
                {
                    return SPDiagnosticsServiceBase.GetLocal<DiagnosticsService>();
                }
            }

            public SPDiagnosticsCategory this[CategoryId id]
            {
                get
                {
                    return Areas[DiagnosticsAreaName].Categories[id.ToString()];
                }
            }
        }

        private static TraceSeverity mapTraceSeverity(LogSeverity severity)
        {
            TraceSeverity tr = TraceSeverity.None;
            switch (severity)
            {
                case LogSeverity.Unexpected:
                    tr = TraceSeverity.Unexpected;
                    break;
                case LogSeverity.High:
                    tr = TraceSeverity.High;
                    break;
                case LogSeverity.Monitorable:
                    tr = TraceSeverity.Monitorable;
                    break;
                case LogSeverity.Medium:
                    tr = TraceSeverity.Medium;
                    break;
                case LogSeverity.Info:
                case LogSeverity.Verbose:
                    tr = TraceSeverity.Verbose;
                    break;
                case LogSeverity.VerboseEx:
                    tr = TraceSeverity.VerboseEx;
                    break;
                case LogSeverity.None:
                    tr = TraceSeverity.None;
                    break;
            }
            return tr;
        }
    }

A simple call in catch will, serve the purpose

catch (Exception ex)
            {
                throw new Exception("Exception Occurred, Details :" + " + ex.message.tostring());
            }

No comments:

Post a Comment