Skip to main content

How to get the current published item through sharepoint list in C#

Dear All
Today i successfully get the current published item from the sharepoint list in my custom webpart

here i s a code for it
**********************
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.Text;
using Microsoft.SharePoint.WebPartPages;
using System.Globalization;
using System.Threading;


namespace customwebpart.slider
{

    [ToolboxItemAttribute(false)]
    public partial class sliderUserControl : UserControl
    {

        /// <summary>
        /// ListName
        /// </summary>
        [WebBrowsable(true)]
        [WebDescription("HomeSliderListName")]
        [WebDisplayName("HomeSlider List Title")]
        //[ResourcesAttribute("WebUrl", "Custom", "WebUrlDescription")]
        [Personalizable(PersonalizationScope.Shared)]
        public string ListName
        {
            get;
            set;
        }

        protected CultureInfo currentCulture
        {
            get { return Thread.CurrentThread.CurrentCulture; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            ListName = HttpContext.GetGlobalResourceObject("wss", "[Your List Name]", currentCulture).ToString();

            if (String.IsNullOrEmpty(this.ListName))
            {
                lblMessage.Text = "Edit the web part and specify a Picture Library to display.";
                lblMessage.Visible = true;
                return;
            }

            using (SPSite Site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb Web = Site.OpenWeb(SPContext.Current.Web.ID))
                {
                    SPList Slider = Web.Lists[ListName];
                    SPListItemCollection Items = Slider.Items;
                    StringBuilder largeImages = new StringBuilder();
                    StringBuilder smallImages = new StringBuilder();
                    int j=1;

                    if (Slider.EnableVersioning)
                        {
                            foreach (SPListItem item in Items)
                            {

                                SPListItemVersionCollection versions = item.Versions;
                                for (int i = 0; i < versions.Count; i++)
                                {
                                    // Check if version is published

                                    if (versions[i].Level == SPFileLevel.Published && versions[i].IsCurrentVersion == true)
                                    {
                                        smallImages.Append("<div title='" + j + "'> &nbsp;</div>");
                                        largeImages.Append(string.Format(currentCulture, "<a href='" + (string)versions[i]["URL"] + "' target='_blank'> <img src='/{0}' alt='{1}' title='{1}' id='{2}'/></a>", (string)item["Required Field"], (string)versions[i]["Title"], ID));
                                        j = j + 1;
                                    }
                                }
                            }
                        }
                       
                        else
                        {
                            foreach (SPListItem item in Items)
                            {
                                smallImages.Append("<div title='" + j + "'> &nbsp;</div>");
                                largeImages.Append(string.Format(currentCulture, "<a href='" + (string)item["URL"] + "' target='_blank'> <img src='/{0}' alt='{1}' title='{1}' id='{2}'/></a>", (string)item["Required Field"], (string)item["Title"], ID));
                                j = j + 1;
                            }
                        }

                   

                 
                    LargeImageContainer.Text = largeImages.ToString();
                    smallImageContainer.Text = smallImages.ToString();
                    this.lbllang.Text = currentCulture.Name.ToLower(currentCulture);
                }
            }

        }
       
    }
}

I hope it save your time because i found the solution after long search 

Regards 

Comments

Popular posts from this blog

Updatepanel or Enable Ajax in SharePoint webpart

Dear All It is really giving me a hard to get this techniques if you want to run the update panel in sharepoint 2013 webpart then you need to initialize the script manage by code   protected override void OnInit(EventArgs e)         {             base.OnInit(e);             InitializeControl();             // Register the ScriptManager             ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);             if (scriptManager == null)             {                 scriptManager = new ScriptManager();                 scriptManager.ID = "ScriptManager1";                 scriptManager.EnablePartialRendering = true;                 Controls.AddAt(0, scriptManager);             }         } In ascx page you need to add the following  <asp:UpdateProgress ID="UpdateProgress1" runat="server">     <ProgressTemplate>         <h1>Your progress value</h1>     </ProgressTemplate> </asp:U

[Solved] SharePoint 2013 And Adobe Reader Problem : The URL you have provided could not be reached. Please verify that the URL is correct and that the network location is reachable

Dear All  When trying to open a PDF file from a mapped drive in SharePoint 2010.  You might see the following error message. The URL you have provided could not be reached. Please verify that the URL is correct and that the network location is reachable. 1. Open the registry. 2. Go to HKLM Local Software\SOFTWARE\Policies\Adobe\\\FeatureLockDown. 3. Create a key called cSharePoint. 4. Create a DWORD value called bDisableSharePointFeatures. 5. Set its value to 1. Regards Rashid Imran Bilgrami CEO Best visualization http://www.bestvisualization.com  

SharePoint: A Complete Guide to Getting and Setting Fields using C#

Original article https://social.technet.microsoft.com/wiki/contents/articles/21801.sharepoint-a-complete-guide-to-getting-and-setting-fields-using-c.aspx Introduction This article demonstrates how to set and get the various SPField types for a SharePoint list using C#. The examples demonstrated set and get fields from an item that belongs to a custom list. The custom list contains a number of fields, and all the fields are named based on the type of field they are. For example, there is a Text field, which has been named, textfield. This is depicted in the following picture:   Applies To The examples demonstrated below are tested with and apply to the following versions of SharePoint: SharePoint 2010 SharePoint 2013 Get the List, and the first SPListItem This is the basic code for getting an item. If the list has at least one item, the first item is retrieved, otherwise a new item is created. var web = SPContext.Current.Site.RootWeb; var list = web.Lists.Try