Sunday, February 17, 2013

Adding webpart Propert using editor part and fetching list item title with styling


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.WebControls;

using Microsoft.SharePoint;

using ZS.CodeChallenge.V1.common;

using System.Web;

using Microsoft.SharePoint.WebPartPages;


namespace addingwebpartproperty

{

class ListDisplayThemeEditorPart:EditorPart

{





#regioncontrols in webpart property

private DropDownListddlThemeLists = new DropDownList();

private DropDownListddlListName = new DropDownList();

private TextBoxtxtBoxItemCount = new TextBox();

#endregion

#regionConstructor

public ListDisplayThemeEditorPart(string webPartID)

{

ID = webPartID + "CustomEditorPart";

}

#endregion

public override bool ApplyChanges()

{

try

{

EnsureChildControls();

#region Assign values

ListDisplayThemeWebPart webPart = (ListDisplayThemeWebPart)WebPartToEdit;

webPart.ThemeName = ddlThemeLists.SelectedValue;

webPart.DisplayListName = ddlListName.SelectedValue;

webPart.ItemLimit = txtBoxItemCount.Text;

#endregion

return true;

}

catch (Exceptionex)

{

ULSLogging.LogError("CodeChallege", ex.Message);

return false;

}

}

public override void SyncChanges()

{

try

{

EnsureChildControls();

ListDisplayThemeWebPart webPart = (ListDisplayThemeWebPart)WebPartToEdit;

#region Theme Dropdown

//string[] lstThemeColors = Convert.ToString(HttpContext.GetGlobalResourceObject("CodeChallengeResource", "WebPartThemes")).Split(new char[] {','});// { "Maroon", "Green", "Blue" };

string[] lstThemeColors = { "Maroon", "Green","Blue" };

if (ddlThemeLists.Items.Count > 0)

{

ddlThemeLists.Items.Clear();

}

foreach (stringcolor in lstThemeColors)

{

ddlThemeLists.Items.Add(new ListItem(color, color));

}

if (ddlThemeLists.Items.Count > 0&& webPart.ThemeName != null)

{

foreach (ListItemliTheme in ddlThemeLists.Items)

{

if (webPart.ThemeName.Equals(liTheme.Text))

{

ddlThemeLists.SelectedValue = liTheme.Value;

break;

}

}

}

#endregion

#region List Dropdown

if (ddlListName.Items.Count > 0)

{

ddlListName.Items.Clear();

}

SPListCollection listCollection = SPContext.Current.Web.Lists;

foreach (SPListlist in listCollection)

{

ddlListName.Items.Add(new ListItem(list.Title, list.Title));

}

if (ddlListName.Items.Count > 0&& webPart.DisplayListName != null)

{

foreach (ListItem itemList in ddlListName.Items)

{

if (webPart.DisplayListName.Equals(itemList.Text))

{

ddlListName.SelectedValue = itemList.Value;

break;

}

}

}


#endregion


#region Item Count Textbox

if (!String.IsNullOrEmpty(webPart.ItemLimit))

{

txtBoxItemCount.Text = webPart.ItemLimit;

}

else

{

txtBoxItemCount.Text = "5";

}

#endregion


}

catch (Exceptionex)

{

ULSLogging.LogError("CodeChallege", ex.Message);

}


}

protected override void CreateChildControls()

{

try

{

#region Add controls to Webpart property

this.Title = "Custom Properties";

this.Controls.Add(new Literal { Text = "List" + ":<br/><br/>" });

this.Controls.Add(ddlListName);

this.Controls.Add(new Literal { Text = "<br/><br/>" });

this.Controls.Add(new Literal { Text = "Theme" + ":<br/><br/>" });

this.Controls.Add(ddlThemeLists);

this.Controls.Add(new Literal { Text = "<br/><br/>" });


this.Controls.Add(new Literal { Text = "Item Count" + ":<br/><br/>" });

this.Controls.Add(txtBoxItemCount);

this.Controls.Add(new Literal { Text = "<br/><br/>" });

#endregion

base.CreateChildControls();

}

catch (Exceptionex)

{

ULSLogging.LogError("CodeChallege", ex.Message);

}

}

}

}

WebpartCS file:-

using System;

using System.ComponentModel;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using System.Collections.Generic;

using Microsoft.SharePoint.WebPartPages;

using System.Text;

using ZS.CodeChallenge.V1.common;

using Microsoft.SharePoint.Utilities;


namespace WebPart

{

    [ToolboxItemAttribute(false)]

    public class ListDisplayThemeWebPart : Microsoft.SharePoint.WebPartPages.WebPart

    {

        #region Properties

        private string _themeColor;

        [Personalizable(PersonalizationScope.Shared)]

        [WebPartStorage(Storage.Shared)]

        [WebBrowsable(false)]

        [WebDisplayName("Theme")]

        [WebDescription("Theme")]

        [SPWebCategoryName("Internal")]

        public string ThemeName

        {

            get { return _themeColor; }

            set { _themeColor = value; }

        }


        private string _ListName;

        [Personalizable(PersonalizationScope.Shared)]

        [WebPartStorage(Storage.Shared)]

        [WebBrowsable(false)]

        [WebDisplayName("List Name")]

        [WebDescription("List Name")]

        [SPWebCategoryName("Internal")]

        public string DisplayListName

        {

            get { return _ListName; }

            set { _ListName = value; }

        }


        private string _ItemLimit;

        [Personalizable(PersonalizationScope.Shared)]

        [WebPartStorage(Storage.Shared)]

        [WebBrowsable(false)]

        [WebDisplayName("Item Limit")]

        [WebDescription("Item Limit")]

        [SPWebCategoryName("Internal")]

        public string ItemLimit

        {

            get { return _ItemLimit; }

            set { _ItemLimit = value; }

        }


        #endregion Properties

        public override EditorPartCollection CreateEditorParts()

        {

            try

            {

                #region Adding Custom Property



                ListDisplayThemeEditorPart lstEditorPart = new ListDisplayThemeEditorPart(ID);

                System.Collections.Generic.List<EditorPart> customParts = new System.Collections.Generic.List<EditorPart>();

                customParts.Add(lstEditorPart);

                return new EditorPartCollection(base.CreateEditorParts(), customParts);



                #endregion

            }

            catch (Exception ex)

            {

                ULSLogging.LogError("CodeChallege", ex.Message);

                return null;

            }

        }

        protected override void CreateChildControls()

        {

            try

            {


                throw new HttpException();

                #region Register CSS class


                //Adding css file link on page

                this.Page.Header.Controls.Add(new LiteralControl("<link href='/_layouts/webpaeart/css/ListDisplayStyling.css' rel='stylesheet' type='text/css' />"));


                #endregion


                #region Main DIV to identify theme


                System.Web.UI.HtmlControls.HtmlGenericControl divListDisplayMain = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");

                divListDisplayMain.ID = "divListDisplayMain";


                //Check if Theme is selected in Webpart Property, If not give default theme as Maroon

                if (!String.IsNullOrEmpty(ThemeName))

                {

                    divListDisplayMain.Attributes.Add("class", ThemeName + "_ThemeClass");

                }

                else

                {

                    divListDisplayMain.Attributes.Add("class", "Maroon_ThemeClass");

                }

                this.Controls.Add(divListDisplayMain);


                #endregion


                #region Container DIV for Title and List Items


                //Create DIV for Title and List Itmes DIv

                System.Web.UI.HtmlControls.HtmlGenericControl divListDisplayContainer = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");

                divListDisplayContainer.ID = "divListDisplayContainer";

                divListDisplayContainer.Attributes.Add("class", "sp-CodeChallenge-Container");

                divListDisplayMain.Controls.Add(divListDisplayContainer);


                #endregion


                #region Title DIV


                //Addind List Title

                System.Web.UI.HtmlControls.HtmlGenericControl divListDisplayTitle = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");

                divListDisplayTitle.ID = "divListDisplayTitle";

                divListDisplayTitle.Attributes.Add("class", "sp-CodeChallenge-Title shadow");

                Label labelTitle = new Label();


                //Check if List is selected in edit properties, If not set List selection Text to Title of WebPart

                if (!String.IsNullOrEmpty(DisplayListName))

                {

                    labelTitle.Text = DisplayListName;

                }

                else

                {

                    labelTitle.Text = "Select List";

                }


                divListDisplayTitle.Controls.Add(labelTitle);

                divListDisplayContainer.Controls.Add(divListDisplayTitle);


                #endregion


                #region List Rows DIV


                //Check if list is selected in Web part property

                if (!String.IsNullOrEmpty(DisplayListName))

                {


                    using (SPSite site = new SPSite(SPContext.Current.Web.Url))

                    {

                       

                        using (SPWeb web = site.OpenWeb())

                        {

                           

                            SPList listToFetch = web.Lists.TryGetList(DisplayListName);

                            if (listToFetch != null)

                            {

                                #region query to fetch itmes

                                //fetch only limited items from list

                                String columnToShow = "Title";

                                SPQuery query = new SPQuery();

                                //limit columns to fetch

                                query.ViewFields = "<FieldRef Name='" + columnToShow + "'/>";

                                //limit items to fetch

                                if (String.IsNullOrEmpty(ItemLimit))

                                {

                                    query.RowLimit = 5;

                                }

                                else

                                {

                                    query.RowLimit = Convert.ToUInt32(ItemLimit);

                                }

                                #endregion



                                SPListItemCollection itemCollection = listToFetch.GetItems(query);


                                if (itemCollection != null && itemCollection.Count > 0)

                                {

                                    #region Show Item Count

                                    int itemShowCount = itemCollection.Count;

                                   

                                    System.Web.UI.HtmlControls.HtmlGenericControl divListDisplayItemCount = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");

                                    divListDisplayItemCount.Attributes.Add("class", "sp-CodeChallenge-ItemCount");

                                    Label labelItemCount = new Label();

                                    labelItemCount.Text = "List Items Count:" + listToFetch.ItemCount + "<br/>Items Displaying:" + itemShowCount;

                                    divListDisplayItemCount.Controls.Add(labelItemCount);

                                    divListDisplayContainer.Controls.Add(divListDisplayItemCount);

                                    #endregion


                                    #region Show list items

                                    System.Web.UI.HtmlControls.HtmlGenericControl divListItems = FillListDiv(itemShowCount,columnToShow, itemCollection);

                                    if (divListItems != null)

                                    {

                                        divListDisplayContainer.Controls.Add(divListItems);

                                    }

                                    #endregion

                                }

                                else

                                {

                                    #region No Itmes in List Error Message

                                    System.Web.UI.HtmlControls.HtmlGenericControl divListDisplayNoItems = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");

                                    divListDisplayNoItems.Attributes.Add("class", "sp-CodeChallenge-ItemCount");

                                    Label labelItemCount = new Label();

                                    labelItemCount.Text = "There are no items in list to display.";

                                    divListDisplayNoItems.Controls.Add(labelItemCount);

                                    divListDisplayContainer.Controls.Add(divListDisplayNoItems);

                                    #endregion

                                }

                            }

                        }

                    }

                }

                else

                {

                    #region No list selected Error message

                    System.Web.UI.HtmlControls.HtmlGenericControl divListDisplayNoItems = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");

                    divListDisplayNoItems.Attributes.Add("class", "sp-CodeChallenge-ItemCount");

                    Label labelItemCount = new Label();

                    labelItemCount.Text = "Edit this Web Part to select the list to display.";

                    divListDisplayNoItems.Controls.Add(labelItemCount);

                    divListDisplayContainer.Controls.Add(divListDisplayNoItems);

                    #endregion

                }


                #endregion

            }

            catch (Exception ex)

            {

                ULSLogging.LogError("CodeChallege", ex.Message);

            }


        }

        private System.Web.UI.HtmlControls.HtmlGenericControl FillListDiv(int itemShowCount,String columnToShow, SPListItemCollection itemCollection)

        {

            try

            {

                #region create Display list div


                System.Web.UI.HtmlControls.HtmlGenericControl divListItems = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");

                divListItems.ID = "divListItems";


                for (int i = 0; i < itemShowCount; i++)

                {

                    #region Item Div

                    System.Web.UI.HtmlControls.HtmlGenericControl divListItem = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");

                        divListItem.ID = "divListItem";

                        divListItem.Attributes.Add("class", "sp-CodeChallenge-Item");


                        StringBuilder displayString = new StringBuilder();


                        if (itemCollection[i][columnToShow] != null)

                        {

                            displayString.Append(Convert.ToString(itemCollection[i][columnToShow]));

                        }

                        else

                        {

                            displayString.Append(columnToShow+" is NULL");

                        }

                        displayString.Append("<br/>");


                        Label labelItemTitle = new Label();

                        labelItemTitle.Text = Convert.ToString(displayString);

                        divListItem.Controls.Add(labelItemTitle);

                    #endregion

                    divListItems.Controls.Add(divListItem);

                }
                return divListItems;
             #endregion

            }

            catch (Exception ex)

            {

                ULSLogging.LogError("CodeChallege", ex.Message);

                return null;

            }
        }
    }
}


css:-
.sp-CodeChallenge-Contain{
 font-family:Arial;
width: 342px;
position: relative;
border: 2px solid #e6e7e7 !important;
float:left;
padding:50px 20px 0px 20px;
margin-top:20px;
margin-left:20px;
margin-bottom:15px;
}

.sp-CodeChallenge-Title

{

font-family: Georgia;
font-size: 16px;
font-weight: bold;
padding: 12px 20px;
color: #FFFFFF;
position: absolute;
top: -15px;
left: -15px;
box-shadow:5px 5px 5px #CCCCCC;
min-width:155px !important;
}

.shadow

{

-moz-box-shadow: 5px 5px 5px #999999;
-webkit-box-shadow: 5px 5px 5px #999999;
box-shadow: 5px 5px 5px #999999;
-ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=6, Direction=135, Color='#999999')";
filter: progid:DXImageTransform.Microsoft.Shadow(Strength=6, Direction=135, Color='#999999');
}

.sp-CodeChallenge-Item

{

font-family: Georgia;
font-size: 14px;
font-weight: bold;
padding:8px 0 8px 0;
color: #840f08 !important;
border-top:2px solid #e6e7e7;
}

.sp-CodeChallenge-ItemCount

{

font-family: Georgia;
font-size: 12px;
padding:0 0 8px 0;
color: #840f08;
text-align:right;
}

.Maroon_ThemeClass .sp-CodeChallenge-Title
{

background:#840f08;
}

.Blue_ThemeClass .sp-CodeChallenge-Title
{

background:#033788;
}

.Green_ThemeClass .sp-CodeChallenge-Title
{

background:#148d42;
}

.Maroon_ThemeClass .sp-CodeChallenge-Item
{

color: #840f08 !important;
}

.Blue_ThemeClass .sp-CodeChallenge-Item
{

color: #033788 !important;
}

.Green_ThemeClass .sp-CodeChallenge-Item
{

color: #148d42 !important;
}

.Maroon_ThemeClass .sp-CodeChallenge-ItemCount
{

color: #840f08 !important;
}

.Blue_ThemeClass .sp-CodeChallenge-ItemCount
{

color: #033788 !important;
}

.Green_ThemeClass .sp-CodeChallenge-ItemCount
{

color: #148d42 !important;
}

No comments:

Post a Comment

SharePoint document metadata not updating

I faced a weird issue today, Metadata for document which has lookup column was not updating even after saving the item. There was no erro...