Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Friday, 13 June 2014

Save Exceptions In SharePoint List Programmatically

protected void ExceptionLog(Exception ex)
        {
            using (SPSite oSite = new SPSite(siteURL))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    SPList olist = oWeb.Lists["ErrorLog"];
                    oWeb.AllowUnsafeUpdates = true;
                    SPListItem itemToAdd = olist.Items.Add();
                    itemToAdd["HelpLink"] = ex.HelpLink;
                    itemToAdd["Message"] = ex.Message;
                    itemToAdd["Source"] = ex.Source;
                    itemToAdd["StackTrace"] = ex.StackTrace;
                    itemToAdd["TargetSite"] = ex.TargetSite;
                    SPUser oUser = oWeb.EnsureUser(oWeb.CurrentUser.LoginName);
                    itemToAdd["UserName"] = oUser;
                    itemToAdd["Module"] = ex.TargetSite.DeclaringType;
                    itemToAdd.Update();
                    oWeb.AllowUnsafeUpdates = false;
                    //Response.Redirect("/SitePages/Error.aspx", false);
                }
            }
        }
Please provide your feedback in the comments section above. Please don't forget to follow.