Saturday, January 17, 2015

Wiki Report

Hi,

Before going to the customization part, let me explore on the folder structure in the sub site.
There is a sub site called wiki site under the site collection.
In this, there are two folders called Enabling Functions and General.
In Enabling functions folder, there are sub folders, in those sub folders there are files.
In General folder, there are some files.
For every folder, there would be a SPOC that we can get the details from DB.
SPOC details would be in the table as below
But I should access the same through one of the stored procedure 
Now coming to requirement, should generate the report through below web part
Now report should be generated in two formats i.e.
1)If we select Report type as Consolidated, report should be in the below format.
-Under Category it should display the sub folder names of Enabling Functions folder, folder name of General.
-Under No of questions added, it should display the total files count in the respective folder.
-Under New questions added, it should display the count of files within the selected dates based on the Page Created Date.
-Under SPOC column, it should display the SPOCS of that folder from DB
Before writing the code in button click event, class has been instantiated to get the SPOC data.
We are logging the logs into the DB

WikiFAQsReport_BAL objWikiFAQsReport = new WikiFAQsReport_BAL();
public class WikiFAQsReport_BAL
    {
        WikiFAQsReport_DAL dataObj = new WikiFAQsReport_DAL();
        public DataSet GetAuthorNames(string type, string businessFunction)
        {
            try
            {
                return dataObj.GetAuthorNames(type, businessFunction);
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void WriteLogStatusFile(string PageName, string MethodName, String message)
        {
            StringBuilder Message = new StringBuilder();
            Message.Append("Page Name - ");
            Message.Append(PageName);
            Message.Append(" ");
            Message.Append("PageMethod Name - ");
            Message.Append(MethodName);
            Message.Append("message");
            Message.Append(message);

            //Storing it into DB   

            dataObj.LogStatus(PageName, MethodName, message);
        }
    }
public class WikiFAQsReport_DAL
    {
        public static string NucleusConnectionString = "Data Source=domain\\connection;Initial Catalog=DBnames;User ID=userID;Password=pswd";  // QA
        public DataSet GetAuthorNames(string type, string businessFunction)
        {
            try
            {
                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@BUSINESS_TYPE"SqlDbType.NVarChar);
                par[0].Value = type;
                par[1] = new SqlParameter("@BUSINESS_FUNCTION"SqlDbType.NVarChar);
                par[1].Value = businessFunction;
                DataSet ds = SqlHelper.ExecuteDataset(NucleusConnectionString, CommandType.StoredProcedure, "PROC_procedure_name", par);

                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public void LogStatus(string PageName, string MethodName, string message)
        {
            try
            {
                SqlParameter[] par = new SqlParameter[3];
                par[0] = new SqlParameter("@PageName"SqlDbType.VarChar);
                par[0].Value = PageName;
                par[1] = new SqlParameter("@MethodName"SqlDbType.VarChar);
                par[1].Value = MethodName;
                par[2] = new SqlParameter("@Error"SqlDbType.VarChar);
                par[2].Value = message;
                DataSet ds = SqlHelper.ExecuteDataset(NucleusConnectionString, CommandType.StoredProcedure, "PROC_procedurename_ErrorLog", par);
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
For this I have written the code as below in the export to excel button click event

private void ConsolidatedReport()
        {
            string strwikisiteurl = SPContext.Current.Site.Url + "/WikiSite/";
            using (SPSite wikisite = new SPSite(strwikisiteurl))
            {
                using (SPWeb wikiweb = wikisite.OpenWeb())
                {
                    SPList list = wikiweb.Lists["Pages"];
                    dt.Columns.Add("Category");
                    dt.Columns.Add("No of questions added");
                    dt.Columns.Add("New Questions added");
                    dt.Columns.Add("SPOC");
                    foreach (SPFolder folder in list.RootFolder.SubFolders)
                    {
                        if (folder.Name != "Forms")
                        {
                            if (folder.Name == “General”)
                            {
                                dr = dt.NewRow();

                                dr["Category"] = “General”;

                                dr["No of questions added"] = folder.ItemCount.ToString();
                                int filecount = 0;
                                foreach (SPFile file in folder.Files)
                                {
                                    SPListItem item = file.Item;

                                    if ((file.TimeCreated >= dtFromDate.SelectedDate.Date) && (file.TimeCreated <= dtToDate.SelectedDate.Date))
                                    {
                                        filecount++;
                                    }
                                    if (filecount >= 1)
                                    {
                                        dr["New Questions added"] = filecount.ToString();
                                    }
                                    else
                                    {
                                       dr["New Questions added"] = filecount.ToString();
                                    }
                                    DataSet ds = objWikiFAQsReport.GetAuthorNames(“General”, folder.Name.ToString());
                                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                                    {
                                        if (ds.Tables[0].Rows.Count == 1)
                                        {
                                            getDisplayName(ds);
                                            SPUser user = web.EnsureUser(SPOCMId);
                                            dr["SPOC"] = user.Name;
                                        }
                                        else if (ds.Tables[0].Rows.Count > 1)
                                        {
                                            getDisplayName(ds);
                                            SPUser SPOCuser = web.EnsureUser(SPOCMId);
                                            SPOCMId = SPOCuser.Name;
                                            for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
                                            {
                                                SPUser user = web.EnsureUser(ds.Tables[0].Rows[i]["SPOCMid"].ToString());
                                                if (user != null)
                                                {
                                                    SPOCMlIds = user.Name;
                                                }
                                                SPOCMId = SPOCMId + Constants.Comma + SPOCMlIds; ;
                                            }
                                            dr["SPOC"] = SPOCMId.ToString();
                                        }
                                    }

                                }
                                dt.Rows.Add(dr);
                                dt.AcceptChanges();
                            }
                            else if (folder.Name == “Enabling Functions”)
                            {

                                foreach (SPFolder subfolder in folder.SubFolders)
                                {
                                    if (subfolder.Name != "Forms")
                                    {
                                        if (subfolder.Files.Count > 0)
                                        {

                                            int filecount = 0;
                                            foreach (SPFile file in subfolder.Files)
                                            {
                                                dr = dt.NewRow();

                                                dr["Category"] = subfolder.Name;

                                                dr["No of questions added"] = subfolder.ItemCount.ToString();

                                                SPListItem item = file.Item;

                                                if ((file.TimeCreated >= dtFromDate.SelectedDate.Date) && (file.TimeCreated <= dtToDate.SelectedDate.Date))
                                                {

                                                    filecount++;

                                                }
                                                if (filecount >= 1)
                                                {
                                                    dr["New Questions added"] = filecount.ToString();
                                                }
                                                else
                                                {
                                                    dr["New Questions added"] = filecount.ToString();
                                                }


                                            }

              DataSet ds = objWikiFAQsReport.GetAuthorNames(folder.Name, subfolder.Name);

                                            if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                                            {

                                                if (ds.Tables[0].Rows.Count == 1)
                                                {
                                                    getDisplayName(ds);
                                                    SPUser user = web.EnsureUser(SPOCMId);
                                                    dr["SPOC"] = user.Name;

                                                }
                                                else if (ds.Tables[0].Rows.Count > 1)
                                                {
                                                    getDisplayName(ds);
                                                    SPUser SPOCuser = web.EnsureUser(SPOCMId);
                                                    SPOCMId = SPOCuser.Name;
                                                    for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
                                                    {
                                                        SPUser user = web.EnsureUser(ds.Tables[0].Rows[i]["SPOCMid"].ToString());
                                                        if (user != null)
                                                        {
                                                            SPOCMlIds = user.Name;
                                                        }
                                                        SPOCMId = SPOCMId + Constants.Comma + SPOCMlIds; ;
                                                    }
                                                    dr["SPOC"] = SPOCMId.ToString();
                                                }
                                            }
                                            dt.Rows.Add(dr);
                                            dt.AcceptChanges();
                                        }
                                    }
                                }
                            }
                        }
                    }

                      ExportToExcel(dt);            
                    
                    
                }
            }
        }
private void getDisplayName(DataSet ds)
        {
            authorName = ds.Tables[0].Rows[0]["SPOCMid"].ToString();
            SPUser user = web.EnsureUser(authorName);
            SPOCMId = authorName;
        }
private void ExportToExcel(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {

                //excel file name         
                string filename = "WikiReport" + dtFromDate.SelectedDate.ToString("dd/MM/yy") + "-" + dtToDate.SelectedDate.ToString("dd/MM/yy") + ".xls";


                DataGrid dgGrid = new DataGrid();
                dgGrid.DataSource = dt;
                dgGrid.DataBind();

                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

                dgGrid.RenderControl(hw);            

                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.AddHeader("content-disposition"string.Format("attachment; filename={0}", filename));
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                //render the htmlwriter into the response 
                HttpContext.Current.Response.Write(tw.ToString());               
                HttpContext.Current.Response.End();             
              
            }
           
        }
private void getDisplayName(DataSet ds)
        {
            authorName = ds.Tables[0].Rows[0]["SPOCMid"].ToString();
            SPUser user = web.EnsureUser(authorName);
            SPOCMId = authorName;
        }

I was able to get the Consolidated report as below
2)Now another format is Question Wise Report as below.
-Under Question column files of General ,Enabling Functions sub folder files should be displayed
-Under View Count column, version count within the selected dates should  be displayed
-Under Last Update at each question level column, file modified date should be displayed.
-Under SPOCS column, respective SPOCS should be displayed from the DB.

...For this I am calling method in the button click event as below
private void QuestionWiseReport()
        {
            string strwikisiteurl = SPContext.Current.Site.Url + "/WikiSite/";
            using (SPSite wikisite = new SPSite(strwikisiteurl))
            {
                using (SPWeb wikiweb = wikisite.OpenWeb())
                {
                    SPList list = wikiweb.Lists["Pages"];
                    dt.Columns.Add("Category");
                    dt.Columns.Add("Question");
                    dt.Columns.Add("View count at each question level");
                    dt.Columns.Add("Last update at each question level");
                    dt.Columns.Add("SPOC");

                    foreach (SPFolder folder in list.RootFolder.SubFolders)
                    {
                        if (folder.Name != "Forms")
                        {
                            if (folder.Name == “General”)
                            {
                                foreach (SPFile file in folder.Files)
                                {
                                    //if(file.Name=="TestQue1.aspx")
                                    //dr["Last update at each question level"] = file.TimeLastModified;
                                    GenerateRecords(file, folder.Name, folder.Name);
                                    //dr["Last update at each question level"] = file.TimeLastModified;
                                }
                            }
                            else if (folder.Name == “Enabling Functions”)
                            {
                                foreach (SPFolder subfolder in folder.SubFolders)
                                {
                                    if (subfolder.Name != "Forms")
                                    {
                                        if (subfolder.Files.Count > 0)
                                        {
                                            foreach (SPFile file in subfolder.Files)
                                            {
                                                //if (file.Name == "TestQue1.aspx")
                                                GenerateRecords(file, folder.Name, subfolder.Name);
                                                //dr["Last update at each question level"] = file.TimeLastModified;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    ExportToExcel(dt);
                }
            }
        }
private void GenerateRecords(SPFile file, string folder, string subfolder)
        {
            try
            {
                SPListItem item = file.Item;
                dr = dt.NewRow();              
                dr["Question"] = file.Name.ToString();
                dr["Last update at each question level"] = file.TimeLastModified;
                //dr["Last update at each question level"] = file.TimeLastModified;

                //foreach (SPFileVersion v in file.Versions)
                //{
                //    dr["Last update at each question level"] = v.Created;
                //}                

                if (folder == “General”)
                {
                    dr["Category"] = “General”;
                    dr["View count at each question level"] = GetViewCount(Convert.ToString(item["Title"]), folder);
                }
                else if ((folder == “Enabling Functions”)
                {
                    dr["Category"] = subfolder;
                    dr["View count at each question level"] = GetViewCount(subfolder + "/" + Convert.ToString(item["Title"]), folder);
                }
                //dr["Last update at each question level"] = file.TimeLastModified;
                DataSet ds = objWikiFAQsReport.GetAuthorNames(folder, subfolder);
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count == 1)
                    {
                        getDisplayName(ds);
                        SPUser user = web.EnsureUser(SPOCMId);
                        dr["SPOC"] = user.Name;

                    }
                    else if (ds.Tables[0].Rows.Count > 1)
                    {
                        getDisplayName(ds);
                        SPUser SPOCuser = web.EnsureUser(SPOCMId);
                        SPOCMId = SPOCuser.Name;
                        for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
                        {
                            SPUser user = web.EnsureUser(ds.Tables[0].Rows[i]["SPOCMid"].ToString());
                            if (user != null)
                            {
                                SPOCMlIds = user.Name;
                            }
                            SPOCMId = SPOCMId + Constants.Comma + SPOCMlIds; ;
                        }
                        dr["SPOC"] = SPOCMId.ToString();

                    }
                }
                dt.Rows.Add(dr);
            }
            catch (Exception ex)
            {
                objWikiFAQsReport.WriteLogStatusFile("Inside Generate Records""Generate Records()""Generate Records: " + ex.Message);
            }
        }

Thursday, January 8, 2015

Control dtToDate referenced by the ControlToCompare property of cmpDates cannot be validated

Hi,

There are two Share point Date time controls with ids dtFromDate,dtToDate.
Have applied the compare validator so that to display message if user selects start date which would be greater than To date.
<asp:CompareValidator ID="cmpDates" runat="server" ControlToValidate="dtFromDate" ControlToCompare="dtToDate" Operator="LessThan" Type="Date"ErrorMessage="From Date should be less than To Date"></asp:CompareValidator>
Started displaying the below error post deploying the web part
From our technical manager, came to know that we need to add extra to the actual id of the SharePoint date time control.
This is required while applying the validation to the SharePoint Date Time control.
So instead of this ControlToValidate="dtFromDate" ControlToCompare="dtToDate" used ControlToValidate="dtFromDate$dtFromDateDate"ControlToCompare="dtToDate$dtToDateDate"

Post that it went fine and validation is working accordingly.

Page Refresh after Downloading the Excel File

Hi,

In our Visual web part I am downloading the excel file by writing the code as below

This has been written in the button click event
Now issue has raised, i.e. if user wants to download the file second time on the same browser.
He has to refresh the browser and click the button to download the file.
Generally to refresh the current page, below is the code snippet that we write to refresh the page.
But the problem is the above code snippet doesn’t get executed once we write the below to download the file
At the same time we are forced to write for downloading the file
Finally I got the solution i.e as below

The reason is












Monday, December 8, 2014

Sharepoint Search Results Page Configuration

Hi,

There would be certain requirement for certain customers like navigating to different search results pages from different pages.

Go the page and follow the below navigation

Site Actions->Edit Page->Search Box->Edit Web Part.

Here in web part Properties make sure that under Miscellaneous category below things have been done.

1)Use Site Level Defaults is unchecked.

2)Provide the search results page url in target Search Url field as below

Saturday, November 29, 2014

Go Home and Go Back functionlaity on SharePoint Search Results Page

Hi,

There was a requirement of Go Back and Go Home functionality on the SharePoint search  results page.

Because once user is redirected to share point search results page user should have certain option to go back to previous page/going back to home page.

To achieve this I have added a content editor web part on search results page through below navigation.

Site Actions->Edit Page->Click on Add a web part->Media and Content->Content Editor web part->Click here to add new content.

Post this clicking on Edit HTML Source on the ribbon.

I have pasted the below html to achieve the functionality
First we have used history.go(-1) in ‘Go Back’ functionality.

But problem with this is it works only in IE browser, didn't work in chrome/Firefox browser.

Monday, November 24, 2014

SPSecurityContext.WindowsIdentity: Could not retrieve a valid windows identity for NTName='domain\username', UPN='username@domain.com'. UPN is required when Kerberos constrained delegation is used.

Hi,

We were getting the below error when we were trying to save the wiki page through SharePoint ribbon.

Error:

SPSecurityContext.WindowsIdentity: Could not retrieve a valid windows identity for NTName='domain\username', UPN='username@domain.com'. UPN is required when Kerberos constrained delegation is used.  

Finally our teammate has found the solution i.e.

In the page layout that we were using we had commented the below table due to certain changes.
At the same time we had commented the table where we were referring to, there it started creating a problem.
Now had uncommented the area and changes as display property to none as below


Sunday, November 2, 2014

Default CheckBox Visibility

Hi,

Earlier behavior on our application was

Once user hovers on list view web part he was able to select the files to download.

But there was change request i.e. by default once user navigates to the page, should be able to see the checkbox to select the file.

For this we have modified the style as below