Friday, October 31, 2014

Sitecore Quick Tip: Detach and Attach Media

ap out an image already used on your website in various places?
If you have ever done this, there are several ways to do this:
  1. [Not recommended] Upload a new media asset or delete the old one, and assigned your assets to various image/files fields across the site. This is time-consuming as you need to re-associated this new media assets into fields on content items.
  2. [Recommended] Instead, leverage your existing media asset and the fact that fields across your site already have points to it by it Item ID. So instead, swap out the asset by clicking the detach button and attached to add new file system asset. Next simply publish the media item and all places on your site that reference this will be updated.

    Detach and Attach Media Buttons

Sitecore: - How to add source section to Droplink and Treelist

Source for DropLink:-

query:fast:/sitecore/content/XXX/ Home//*[@@templateid='{fsdfasdfasdf}']

Source for TreeList:-

/sitecore/content/ccccc/Home





Simple Pagination through query string :-




 1) Add variable to page count like
int itemPageCount = 15;
2) Get the total items
int totalItem = totalSelectedItems.Count;
3) How much pages will be created
int totalPage;
 int totalPageCheck = totalItem % itemsPerPage;
  if (totalPageCheck == 0) totalPage = totalItem / itemsPerPage;
                else totalPage = (totalItem / itemsPerPage) + 1;

4) Set default page number to 1

var pageNumber = 1;

-----------
 if (!string.IsNullOrEmpty(Request.QueryString["page"]))
                {
                    int num;
                    bool res = int.TryParse(Request.QueryString["page"], out num);
                    if (res)
                        pageNumber = num;
                }
                if (isButton)
                {
                    pageNumber = 1;
                }
                if (pageNumber == 1)
                {
                    individualItems = totalSelectedItems.Take(itemsPerPage).ToList();
                }
                else
                {
                    individualItems = totalSelectedItems.Skip(itemsPerPage * (pageNumber - 1)).Take(itemsPerPage).ToList();
                }



5) Write code section to print record from totalSelectedItems

6) Code for generating the querysting

  if (totalItem > itemsPerPage)
                    {
                        SimpleControl textCenterDiv = SimpleControl.RenderSimple(HtmlTextWriterTag.Div,
                                                                                 "text-center", string.Empty);
                        pagination.Controls.Add(textCenterDiv);

                        SimpleControl paginationUl = SimpleControl.RenderSimple(HtmlTextWriterTag.Ul,
                                                                                "pagination", string.Empty);
                        textCenterDiv.Controls.Add(paginationUl);
                        if (pageNumber == 1)
                        {
                            SimpleControl paginationPreviousLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                        "disabled",
                                                                                        string.Empty);
                            paginationUl.Controls.Add(paginationPreviousLi);
                            SimpleControl previousTag = SimpleControl.RenderSimpleLink("<", string.Empty,
                                                                                       string.Empty,
                                                                                       string.Empty);
                            paginationPreviousLi.Controls.Add(previousTag);
                        }
                        else
                        {
                            SimpleControl paginationPreviousLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                        string.Empty,
                                                                                        string.Empty);
                            paginationUl.Controls.Add(paginationPreviousLi);
                            var uri = HttpContext.Current.Request.Url.AbsoluteUri.Split(new char[] { '?' });
                            SimpleControl previousTagClickable = SimpleControl.RenderSimpleLink("<",
                                                                                                uri[0] +
                                                                                                "?page=" +
                                                                                                (pageNumber - 1) + "&segment=" + ddlItems.SelectedItem.Value.Trim() + "&filter=" + ddlletterFilter.SelectedItem.Value.Trim(),
                                                                                                string.Empty,
                                                                                                string.Empty);
                            paginationPreviousLi.Controls.Add(previousTagClickable);
                        }


                        for (int i = 1; i <= totalPage; i++)
                        {
                            var uri = HttpContext.Current.Request.Url.AbsoluteUri.Split(new char[] { '?' });

                            if (pageNumber == i)
                            {
                                SimpleControl paginationLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                    "active", string.Empty);
                                paginationUl.Controls.Add(paginationLi);
                                SimpleControl pageActive = SimpleControl.RenderSimple(HtmlTextWriterTag.A,
                                                                                      string.Empty, string.Empty,
                                                                                      i.ToString());
                                paginationLi.Controls.Add(pageActive);
                            }
                            else
                            {
                                SimpleControl paginationLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                    string.Empty, string.Empty);
                                paginationUl.Controls.Add(paginationLi);
                                SimpleControl pageLink = SimpleControl.RenderSimpleLink(i.ToString(),
                                                                                        uri[0] + "?page=" + i + "&segment=" + ddlItems.SelectedItem.Value.Trim() + "&filter=" +
                                                                                        ddlletterFilter.SelectedItem.Value.Trim(),
                                                                                        string.Empty,
                                                                                        string.Empty);
                                paginationLi.Controls.Add(pageLink);
                            }
                        }
                        if (pageNumber == totalPage)
                        {
                            SimpleControl paginationNextLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                    "disabled", string.Empty);
                            paginationUl.Controls.Add(paginationNextLi);
                            SimpleControl NextTag = SimpleControl.RenderSimpleLink(">", string.Empty,
                                                                                   string.Empty, string.Empty);
                            paginationNextLi.Controls.Add(NextTag);
                        }

                        else
                        {
                            SimpleControl paginationNextLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                    string.Empty, string.Empty);
                            paginationUl.Controls.Add(paginationNextLi);
                            var uri = HttpContext.Current.Request.Url.AbsoluteUri.Split(new char[] { '?' });
                            SimpleControl NextTag = SimpleControl.RenderSimpleLink(">",
                                                                                   uri[0] + "?page=" +
                                                                                   (pageNumber + 1) + "&segment=" + ddlItems.SelectedItem.Value.Trim() + "&filter=" +
                                                                                   ddlletterFilter.SelectedItem.Value.Trim(),
                                                                                   string.Empty, string.Empty);
                            paginationNextLi.Controls.Add(NextTag);
                        }
                    }