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

using System;

usingSystem.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 = newDropDownList();

private TextBoxtxtBoxItemCount = newTextBox();

#endregion

#regionConstructor

publicListDisplayThemeEditorPart(string webPartID)

{

ID = webPartID + “CustomEditorPart”;

}

#endregion

public override boolApplyChanges()

{

try

{

EnsureChildControls();

#region Assign values

ListDisplayThemeWebPartwebPart = (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 voidSyncChanges()

{

try

{

EnsureChildControls();

ListDisplayThemeWebPartwebPart = (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 inlstThemeColors)

{

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

}

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

{

foreach (ListItemliTheme inddlThemeLists.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 inlistCollection)

{

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

}

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

{

foreach (ListItem itemList inddlListName.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 voidCreateChildControls()

{

try

{

#region Add controls to Webpart property

this.Title = “Custom Properties”;

this.Controls.Add(new Literal { Text = “List” + “:

“ });

this.Controls.Add(ddlListName);

this.Controls.Add(new Literal { Text =

“ });

this.Controls.Add(new Literal { Text = “Theme” + “:

“ });

this.Controls.Add(ddlThemeLists);

this.Controls.Add(new Literal { Text =

“ });

this.Controls.Add(new Literal { Text = “Item Count” + “:

“ });

this.Controls.Add(txtBoxItemCount);

this.Controls.Add(new Literal { Text =

“ });

#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

    {

        #regionProperties

        private string_themeColor;

        [Personalizable(PersonalizationScope.Shared)]

        [WebPartStorage(Storage.Shared)]

        [WebBrowsable(false)]

        [WebDisplayName(“Theme”)]

        [WebDescription(“Theme”)]

        [SPWebCategoryName(“Internal”)]

        public stringThemeName

        {

            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 stringDisplayListName

        {

            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 stringItemLimit

        {

            get { return_ItemLimit; }

            set { _ItemLimit = value; }

        }

        #endregionProperties

        public override EditorPartCollection CreateEditorParts()

        {

            try

            {

                #region Adding Custom Property

                ListDisplayThemeEditorPartlstEditorPart = new ListDisplayThemeEditorPart(ID);

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

                customParts.Add(lstEditorPart);

                return newEditorPartCollection(base.CreateEditorParts(), customParts);

                #endregion

            }

            catch (Exceptionex)

            {

                ULSLogging.LogError(“CodeChallege”, ex.Message);

                return null;

            }

        }

        protected override void CreateChildControls()

        {

            try

            {

                throw newHttpException();

                #region Register CSS class

                //Adding css file link on page

                this.Page.Header.Controls.Add(new LiteralControl(“”));

                #endregion

                #region Main DIV to identify theme

                System.Web.UI.HtmlControls.HtmlGenericControldivListDisplayMain = newSystem.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.HtmlGenericControldivListDisplayContainer = newSystem.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.HtmlGenericControldivListDisplayTitle = newSystem.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 (SPSitesite = 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 = newSPQuery();

                                //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

                                SPListItemCollectionitemCollection = listToFetch.GetItems(query);

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

                                {

                                    #region Show Item Count

                                    int itemShowCount = itemCollection.Count;

                                   

                                    System.Web.UI.HtmlControls.HtmlGenericControldivListDisplayItemCount = new System.Web.UI.HtmlControls.HtmlGenericControl(“DIV”);

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

                                    Label labelItemCount = newLabel();

                                    labelItemCount.Text = “List Items Count:” + listToFetch.ItemCount +
Items Displaying:”
+ itemShowCount;

                                    divListDisplayItemCount.Controls.Add(labelItemCount);

                                    divListDisplayContainer.Controls.Add(divListDisplayItemCount);

                                    #endregion

                                    #region Show list items

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

                                    if (divListItems != null)

                                    {

                                        divListDisplayContainer.Controls.Add(divListItems);

                                    }

                                    #endregion

                                }

                                else

                                {

                                    #region No Itmes in List Error Message

                                    System.Web.UI.HtmlControls.HtmlGenericControldivListDisplayNoItems = newSystem.Web.UI.HtmlControls.HtmlGenericControl(“DIV”);

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

                                    Label labelItemCount = newLabel();

                                    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.HtmlGenericControldivListDisplayNoItems = newSystem.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 (Exceptionex)

            {

                ULSLogging.LogError(“CodeChallege”, ex.Message);

            }

        }

        private System.Web.UI.HtmlControls.HtmlGenericControl FillListDiv(int itemShowCount,StringcolumnToShow, SPListItemCollectionitemCollection)

        {

            try

            {

                #region create Display list div

                System.Web.UI.HtmlControls.HtmlGenericControldivListItems = new System.Web.UI.HtmlControls.HtmlGenericControl(“DIV”);

                divListItems.ID = “divListItems”;

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

                {

                    #region Item Div

                    System.Web.UI.HtmlControls.HtmlGenericControldivListItem = new System.Web.UI.HtmlControls.HtmlGenericControl(“DIV”);

                        divListItem.ID = “divListItem”;

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

                        StringBuilder displayString = newStringBuilder();

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

                        {

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

                        }

                        else

                        {

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

                        }

                        displayString.Append(
);

                        Label labelItemTitle = new Label();

                        labelItemTitle.Text = Convert.ToString(displayString);

                        divListItem.Controls.Add(labelItemTitle);

                    #endregion

                    divListItems.Controls.Add(divListItem);

                }
                return divListItems;
             #endregion

            }

            catch (Exceptionex)

            {

                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;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s