Tuesday, February 5, 2013

Get SharePoint Health Analyzer data programmatically

SharePoint health analyzer is a functionality which highlights the problems that are there with the servers in the farm.

As you can see, it highlights as a top notification with view these issues link.

It highlights the problem in different categories like availability, security, performance and configuration.

It also shows if it's the error or it is the warning. If it is the error , it highlights as a red circle with cross in it, if it is warning it shows as a yellow triangle with exclamation mark.

There are certain rules that are already written in Analyzer which can also be enabled or disabled. You can also select should it repair automatically or not. You can also see the schedule defined for that rule. We can also create our own rule and add it here.

When these rules execute, SharePoint health analyzer creates the report and writes it to the list called SPHealthReportsList. This is just a speciall type of list created for the SharePoint health related issues.

The default view for the health issues is set to not to show anything than the severity is not equal to 4 - success.

Now we will have a look on how we can access the information of health report of SharePoint programmatically.

I am showing this as a part of button click, you can write it the way you want.

private void btnHealthAnalyzer_Click(object sender, EventArgs e)
                SPHealthReportsList lstHealth = SPHealthReportsList.Local;

                if (lstHealth != null)
                    SPQuery queryHealthData = new SPQuery();

                          queryHealthData.Query =
                        "<OrderBy><FieldRef Name=\"Created\" /></OrderBy>";

                    SPListItemCollection healthresults
                        = lstHealth.GetItems(queryHealthData);
                    DataTable dt = new DataTable();
                    dt.Columns.Add(new DataColumn("Title"));
                    dt.Columns.Add(new DataColumn("Created"));
                    dt.Columns.Add(new DataColumn("Category"));
                    dt.Columns.Add(new DataColumn(gtfcvb 3"Remedy"));
                    dt.Columns.Add(new DataColumn("FailingService"));
                    dt.Columns.Add(new DataColumn("Severity"));

                    if(healthresults.Count > 0)
                        foreach (SPListItem item in healthresults)
                            DataRow dr = dt.NewRow();
                            dr["Title"] = item["Title"];
                            dr["Created"] = item["Created"];
                            dr["Category"] = item["Category"];
                            dr["Remedy"] = item["Remedy"];
                            dr["FailingService"] = item["Failing Services"];
                            dr["Severity"] = item["Severity"];

                        bindingSource1.DataSource = dt;
                        dataGridView1.DataSource = bindingSource1;                                             
            catch (Exception ex)
                throw ex;

and here is the output that you get

I hope this helps.

No comments:

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com