Showing posts with label SharePoint Server 2010. Show all posts
Showing posts with label SharePoint Server 2010. Show all posts

Wednesday, October 1, 2014

Sharepoint updated created by and modified by forcefully


item["Author"] = new SPFieldUserValue(web, SPContext.Current.Web.CurrentUser.ID, SPContext.Current.Web.CurrentUser.LoginName);
                        item["Editor"] = new SPFieldUserValue(web, SPContext.Current.Web.CurrentUser.ID, SPContext.Current.Web.CurrentUser.LoginName);
                        item.Update();

Wednesday, July 30, 2014

Handling SharePoint exception in ULS log and SharePoint List

Adding log to ULS and also adding to sharepoint list

// adding log to ULS log
public class ULSLogService : SPDiagnosticsServiceBase
    {
        private static string PRODUCT_NAME = "Biraj_Blog";

        private ULSLogService() : base("Biraj Logging Service", SPFarm.Local) { }
        private static ULSLogService logSerivce;
        public static ULSLogService LogSerivce
        {
            get
            {
                if (logSerivce == null)
                {
                    logSerivce = new ULSLogService();
                }
                return logSerivce;
            }
        }

        protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
        {
            List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>{
             new SPDiagnosticsArea(PRODUCT_NAME, new List<SPDiagnosticsCategory>{
                 new SPDiagnosticsCategory("Biraj_Info", TraceSeverity.Verbose, EventSeverity.Information),
                 new SPDiagnosticsCategory("Biraj_Error", TraceSeverity.Unexpected, EventSeverity.Warning),
             })
             };
            return areas;
        }

        public static void DisplayInfo(string ClassMethodName, Exception ex)
        {
            SPDiagnosticsCategory category = ULSLogService.LogSerivce.Areas[PRODUCT_NAME].Categories["Biraj_Info"];
            ULSLogService.LogSerivce.WriteTrace(0, category, TraceSeverity.Verbose, ClassMethodName + " \n-- " + ex.TargetSite + " \n-- " + ex.Message + " \n-- " + ex.StackTrace + " \n-- " + ex.Source + " \n-- " + ex.Data);
            Logs.AddIntoList("Info", ClassMethodName, ex);
        }

        public static void DisplayError(string ClassMethodName, Exception ex)
        {
            SPDiagnosticsCategory category = ULSLogService.LogSerivce.Areas[PRODUCT_NAME].Categories["Biraj_Error"];
            ULSLogService.LogSerivce.WriteTrace(0, category, TraceSeverity.Unexpected, ClassMethodName + " \n-- " + ex.TargetSite + " \n-- " + ex.Message + " \n-- " + ex.StackTrace + " \n-- " + ex.Source + " \n-- " + ex.Data);
            Logs.AddIntoList("Error", ClassMethodName, ex);
        }

    }


// adding log to SharePoint List
public static void AddIntoList(string Title, string ClassMethodName, Exception exception)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite spSite = new SPSite(SPContext.Current.Web.Url))
                    {
                        using (SPWeb spWeb = spSite.OpenWeb())
                        {                           
                            try
                            {
                                spWeb.AllowUnsafeUpdates = true;
                                SPList logsList = spWeb.Lists.TryGetList("Logs");
                                SPListItem newItem = logsList.Items.Add();
                                newItem["Title"] = Title;
                                newItem["MethodName"] = ClassMethodName;
                                newItem["TargetSite"] = exception.TargetSite;
                                newItem["Message"] = exception.Message;
                                newItem["Source"] = exception.Source;
                                newItem["StackTrace"] = exception.StackTrace;
                                newItem["Data"] = exception.Data;   
                                
                                newItem.Update();
                                spWeb.AllowUnsafeUpdates = false;
                            }
                            catch(Exception ex)
                            {
                                
                            }                           
                        }
                    }
                });
            }
            catch (Exception ex)
            {
               
               
            }
        }

Thursday, June 20, 2013

WebProvisioned web event - Feature Stapling - sharepoint 2010

WebProvisioned web event receiver -allow to add new additional logic when a site is created-is a new receiver type in SharePoint 2010. This operates at site collection level only.
<Receivers Scope="Site">
You can specify scope to "Site" or "Web" & acoordingly receiver will act SPSite.EventReceivers or SPWeb.EventReceivers

Feature Stapling -allow to add new additional logic when a site is created- will affect whole farm.

Saturday, June 15, 2013

SharePoint 2010 Property Bag - Read / Write data to Property Bag


Property Bag = hash table of Key-Value pairs

Read / Write data to Property Bag

SPSecurity.RunWithElevatedPrivileges(delegate()
        {
        try
        {
            using (SPSite RootSite = new SPSite(URL))
            {
                using (SPWeb SiteCollection = RootSite.OpenWeb())
                {                   
                    try
                    {
                        SiteCollection.AllowUnsafeUpdates = true;
                       // Get connection string from Property bag
                        if (SiteCollection.AllProperties.ContainsKey("ConnectionString"))
                        {
                            ConnectionString = SiteCollection.AllProperties["ConnectionString"].ToString();
                        }                       
                        // Set siteID in the Property bag
                        SiteCollection.Properties["siteID"] = siteID;
                        SiteCollection.Properties.Update();
                        SiteCollection.AllowUnsafeUpdates = false;                       
                    }
                    catch (Exception ex)
                    {
                      //Handle Exception 
                    }          
                }
            }
        }
        catch(Exception ex)       
        {           
        }
        }); 


http://pbs2010.codeplex.com/