Tuesday, May 15, 2012

Dynamically changing DataList Template in VisaulWebPart

This visual webpart is using a data list to display two different template data. The item template design would change based on one parameter. Additionally use of page data source is also displayed

The ASCX –

Removed the Drop down code:

<table class="TableHeader">

    <tr class="Content">

        <td align="left" valign="middle" class="KnowledgeHeading_LV2">

            Office Template Index

        </td>

        <td class="DLHeader" style="width: 30%; text-align: right; padding-right: 30px; vertical-align: middle;

            padding-top: 10px">

            <asp:Button ID="btnPrev" runat="server" Text="<<" OnClick="btnPrev_Click" />

            <asp:Label ID="lblCurrentPage" runat="server" Text="" />

            <asp:Button ID="btnNext" runat="server" Text=">>" OnClick="btnNext_Click" />

        </td>

    </tr>

</table>

 

<table style="width:857px">

    <tr>

        <td>

            <asp:DataList ID="dlOfficeTemplate" runat="server" GridLines="Both">

                <ItemTemplate>

                    <asp:Literal ID="ltlTemplate" runat="server" Text='<%# GetItemTemplate((DataRowView)Container.DataItem) %>' />

                </ItemTemplate>

            </asp:DataList>

        </td>

    </tr>

    <tr>

        <td align="center" style="font-size: 12px; text-align:center; padding-top:10px;">

            <asp:Label ID="lblMsg" runat="server" Visible="false"></asp:Label>

        </td>

    </tr>

</table>

 

The CS page

Page Data Source:

 

#region Page Data Source

        PagedDataSource pagedDataSource = new PagedDataSource();

        int iPageSize = 7;

        public int currentPage

        {

            get

            {

                object o = this.ViewState["_currentPage"];

                if (o == null)

                    return 0;

                else

                    return (int)o;

            }

            set

            {

                this.ViewState["_currentPage"] = value;

            }

        }

        #endregion

 

 

#region Paging Button Click Next - Previous

        protected void btnPrev_Click(object sender, EventArgs e)

        {

            currentPage -= 1;

            PopulateOfficeTemplates(ddlCompanyTemplates.SelectedItem.Text);

        }

 

        protected void btnNext_Click(object sender, EventArgs e)

        {

            currentPage += 1;

            PopulateOfficeTemplates(ddlCompanyTemplates.SelectedItem.Text);

        }

#endregion

 

 

The rest of the code:

 

#region Page Load

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

                PopulateDDLCompanyList();

        }

        #endregion

#region Populate DDL Company Template

        /// <summary>

        /// Populate templates list in dropdown control

        /// </summary>

        protected void PopulateDDLCompanyList()

        {

            try

            {

                var parent = (ListView_MSOfficeTemplatesLogo)Parent;

                if (parent.SiteURL != null && parent.LibraryName != null)

                {

                    string siteURL = SPContext.Current.Site.Url + parent.SiteURL;

                    string libraryName = parent.LibraryName;

                    using (SPSite site = new SPSite(siteURL))

                    {

                        using (SPWeb web = site.OpenWeb())

                        {

                            SPList spListPress = web.Lists[libraryName];

                            SPQuery sQuery = new SPQuery();

                            sQuery.Query = "<OrderBy><FieldRef Name='Title' Ascending='False'></FieldRef></OrderBy>";

                            DataTable dtCompany = spListPress.GetItems(sQuery).GetDataTable();

                            if (dtCompany != null && dtCompany.Rows.Count > 0)

                            {

                                ddlCompanyTemplates.DataSource = dtCompany.DefaultView.ToTable(true, "Title");

                                ddlCompanyTemplates.DataTextField = "Title";

                                ddlCompanyTemplates.DataValueField = "Title";

                                ddlCompanyTemplates.DataBind();

                                PopulateOfficeTemplates(dtCompany.Rows[0]["Title"].ToString().Trim());

                            }

                        }

                    }

                }

            }

            catch (Exception ex)

            {

                Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Check OfficeTemplatesLogo WebPart - Populate DDL :" + ex.Message);

                throw new SPException("Check OfficeTemplatesLogo WebPart - Populate DDL");

            }

        }

        #endregion

 

        #region DDL Company Templates Selected Index Change

        /// <summary>

        /// Selected index changed event for company templates

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void ddlCompanyTemplates_SelectedIndexChanged(object sender, EventArgs e)

        {

            currentPage = 0;

            PopulateOfficeTemplates(ddlCompanyTemplates.SelectedItem.Text);

        }

        #endregion

 

        #region Populate MS Office Templates

        /// <summary>

        /// Populate Office Templates

        /// </summary>

        /// <param name="strCompanyTemplates"></param>

        protected void PopulateOfficeTemplates(string strCompanyTemplates)

        {

            try

            {

                var parent = (ListView_MSOfficeTemplatesLogo)Parent;

                if (parent.SiteURL != null && parent.LibraryName != null)

                {

                    string siteURL = SPContext.Current.Site.Url + parent.SiteURL;

                    string libraryName = parent.TemplatelibraryName;

                    SPSecurity.RunWithElevatedPrivileges(delegate()

                    {

                        using (SPSite elevatedSite = new SPSite(siteURL))

                        {

                            using (SPWeb elevatedWeb = elevatedSite.OpenWeb())

                            {

                                SPList spListDocumentLib = elevatedWeb.Lists[libraryName];

                                SPQuery sQuery = new SPQuery();

                                if (strCompanyTemplates == "Company Logo")

                                {

                                    iPageSize = 7;

                                    sQuery.Query =

                                    "<Where>" +

                                    "<And>"+

                                         "<Eq><FieldRef Name='IsLogo' /><Value Type='Boolean'>1</Value></Eq>" +

                                         "<Eq><FieldRef Name='CompanyTemplates' /><Value Type='Text'>"+strCompanyTemplates+"</Value></Eq>" +

                                     "</And>" +

                                    "</Where>" +

                                    "<OrderBy><FieldRef  Name='Modified' Ascending='FALSE' /></OrderBy>";

                                }

                                else

                                {

                                    iPageSize = 15;

 

                                    sQuery.Query =

                                    "<Where>" +

                                     "<And>" +

                                        "<Eq><FieldRef Name='CompanyTemplates'/><Value Type='Text'>" + strCompanyTemplates + "</Value></Eq>" +

                                        "<Eq><FieldRef Name='IsLogo' /><Value Type='Boolean'>0</Value></Eq>" +

                                     "</And>" +

                                    "</Where>" +

                                    "<OrderBy><FieldRef  Name='Modified' Ascending='FALSE' /></OrderBy>";

                                }

 

                                DataTable dtItems = spListDocumentLib.GetItems(sQuery).GetDataTable();

                                if (dtItems != null && dtItems.Rows.Count > 0)

                                {

                                    pagedDataSource.DataSource = dtItems.DefaultView;

                                    pagedDataSource.AllowPaging = true;

                                    pagedDataSource.PageSize = iPageSize;

                                    pagedDataSource.CurrentPageIndex = currentPage;

                                    btnPrev.Enabled = (!pagedDataSource.IsFirstPage);

                                    btnNext.Enabled = (!pagedDataSource.IsLastPage);

                                    lblCurrentPage.Text = "Page: " + (currentPage + 1).ToString() + " of " + pagedDataSource.PageCount.ToString();

                                    dlOfficeTemplate.DataSource = pagedDataSource;

                                    dlOfficeTemplate.DataBind();

                                    lblMsg.Visible = false;

                                }

                                else

                                {

                                    dlOfficeTemplate.DataBind();

                                    lblMsg.Visible = true;

                                    lblMsg.Text = "Not Data Found";

                                }

 

                            }

                        }

                    });

                }

            }

            catch (Exception ex)

            {

                PortalLog.LogString("Exception OfficeTemplatesLogo WebPart - Populate DataList :" + ex.Message);

                throw new SPException("Check OfficeTemplatesLogo WebPart - Populate DataList");

            }

        }

        #endregion

 

        #region Data List Protected Methods

        protected string GetImageToDisplayValue(DataRowView oView)

        {

            string strDisplayHTML = string.Empty;

            //strDisplayHTML = oView["LinkFilename"].ToString();

            string strImageFileName = oView["LinkFilename"].ToString();

            var parent = (ListView_MSOfficeTemplatesLogo)Parent;

            string siteURL = SPContext.Current.Site.Url + parent.SiteURL;

            string libraryName = parent.TemplatelibraryName;

            string strImageUrl = siteURL + "/" + libraryName + "/" + strImageFileName;

            return strImageUrl;

        }

 

        protected string GetItemTemplate(DataRowView oView)

        {

            var parent = (ListView_MSOfficeTemplatesLogo)Parent;

            string siteURL = SPContext.Current.Site.Url + parent.SiteURL;

            string libraryName = parent.TemplatelibraryName;

            string strFilename = oView["LinkFilename"].ToString();

            string strTitle = oView["Title"].ToString();

            string strImageUrl = siteURL + "/" + libraryName + "/" + strFilename;

            string strTemplateText = string.Empty;

 

            if (ddlCompanyTemplates.SelectedItem.Text == "Company Logo")

            {

                strTemplateText = string.Format(@"

             <table id='tblOfficeTemplateLogo' cellpadding='0' cellspacing='0' width='850px'>

                        <tr style='height:50px;'>

                            <td align='center' style='width: 450px; text-align: center; vertical-align: middle;'>

                              <img id='imgLogo' src='{0}' alt='{1}' Target='_blank' height='44px'/>

                            </td>

                            <td style='width: 400px; text-align:left; vertical-align: middle;'>

                            <a id='hlnkName' ToolTip='{1}' Target='_blank' href='{0}'>{1}</a>

                            </td>

                        </tr>

                    </table>", strImageUrl, strTitle);

            }

            else

            {

                strTemplateText = string.Format(@"

             <table id='tblOfficeTemplate' cellpadding='0' cellspacing='0' width='850px'>

                        <tr style='height:25px;'>

                            <td style='width: 450px; text-align:left; vertical-align: middle;'>

                                <span id='doc'>{0}</span>

                            </td>

                            <td style='width: 400px; text-align:left; vertical-align: middle;'>

                            <a id='hlnkName' ToolTip='{1}' Target='_blank' href='{2}'>{1}</a>

                            </td>

                        </tr>

                    </table>", strFilename, strTitle, strImageUrl);

            }

            return strTemplateText;

        }

        #endregion

 

 

Container.DataItem Method returns the expected return html.

 

Thanks for reading..

If you have a more elegant solution/suggestion – please post a comment… I’ll be happy to hear.


...HaPpY CoDiNg

Partha (Aurum)

 

Ref:

http://www.devx.com/vb2themax/Article/19908

http://weblogs.asp.net/rajbk/archive/2004/07/20/what-s-the-deal-with-databinder-eval-and-container-dataitem.aspx

 

 

Wednesday, May 9, 2012

SharePoint 2010 Timer Service Stuck Stopping

Error
 
Health Analyzer states that one or more servers is not responding and on investigation you try to restart the SPTimerV4 service and at that point it sticks in the “Stopping” mode
 
clip_image001

Fix
 
Open Task Manager, Go to Services and find the SPTimerV4 Service - this will be in the stopping state
 
Right click the service select Go To Process, this will switch to the process screen and should highlight OWSTIMER.EXE - if you can't see this make sure the Show Processes from all users is ticked and that display it regardless of the account it is running with, Right Click OWSTIMER.EXE and select End Process.
 
The SharePoint 2010 Timer service should now be showing as Stopped, right click the service and select Start and you should be good to go!
 
Just in case you need to try and stop and start the timer service from the command line you can also try these:
 
SharePoint 2010: net stop
 
SPTimerV4 net start SPTimerV4
 
I’ve found that this issue happens more during deploying .wsps, the wsps  won’t complete unless the timer service on all machines is running correctly.

Thanks for reading..

If you have a more elegant solution/suggestion – please post a comment… I’ll be happy to hear.
...HaPpY CoDiNg

Partha (Aurum)

Ref:

http://apartha77.blogspot.com/2012/05/problem-deploying-solutionenumdeploymen.html



Monday, May 7, 2012

Problem deploying a solution–enumDeployment status never changes

This is a kind of problem you will face if you are working with multiple WFE and app Servers. In my case I had spent the whole day to figure the problem. The solution that worked in my case was to restart the timer service in all the app and WFE servers.

The two services needs to restarted:

clip_image002

Thanks for reading..

If you have a more elegant solution/suggestion – please post a comment… I’ll be happy to hear.
...HaPpY CoDiNg

Partha (Aurum)

Sunday, May 6, 2012

How to View Core Search Results WebPart’s XML Data

The search component returns the search results to the requesting page as XML data. The Search Results Web Part then determines how to display these results, based on the Web Part’s display properties, and the XSLT Transformation (XSLT) specified in that Web Part's XSL property.

You can Edit/modify the search results view by modifying the XSL transform code for a particular Web Part. Edit/modify the web part property, under Display property un-check Use Location Visualization, the XSL Editor button gets activated. Click the button and add the below xml in. 

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="*"/>
</xsl:template>
</xsl:stylesheet>




The xml is displayed instead of xsl



Thanks for reading..



If you have a more elegant solution/suggestion – please post a comment… I’ll be happy to hear.





...HaPpY CoDiNg



Partha (Aurum)



Ref:



http://www.myfatblog.co.uk/



http://ybbest.wordpress.com/2012/03/13/how-to-customise-search-core-results-web-part-part1/



http://msdn.microsoft.com/en-us/library/ms584121(v=office.12).aspx



http://msdn.microsoft.com/en-us/sp2010devtrainingcourse_extendingsharepointsearchlab_topic3



http://msdn.microsoft.com/en-us/library/ms584121(v=office.12).aspx



http://msdn.microsoft.com/en-us/library/ms546985(v=office.12).aspx



http://msdn.microsoft.com/en-us/library/ms584121(v=office.12).aspx



http://markbucayan.appspot.com/xslt/index.html