Tuesday, January 3, 2012

Sample code working with Links List Using SharePoint Object Model.

Global Declaration
DataSet dsetLinks = new DataSet();
DataTable dtLinks = new DataTable();
string strStyleValign = string.Empty;
RenderMethod
writer.Write("<Table width='100%' style='"+strStyleValign+"'>");
                    writer.Write("<Tr>");
                    writer.Write("<Td>");
                    sgvLinks.RenderControl(writer);
                    writer.Write("</Td>");
                    writer.Write("</Tr>");
                    writer.Write("</Table>");
CreateChildControlsMethod
sgvLinks = new SPGridView();
                    sgvLinks.ID = "sgvLinks";
                    sgvLinks.AutoGenerateColumns = false;
                    sgvLinks.Attributes.Add("runat", "server");
                    this.Controls.Add(sgvLinks);
                    BoundField bUrl = new BoundField();
                    bUrl.DataField = "Hyperlink";
                    bUrl.Visible = false;
                    sgvLinks.Columns.Add(bUrl);
                    HyperLinkField HLinkName = new HyperLinkField();
                    HLinkName.DataTextField = "HyperlinkDescription";
                    HLinkName.DataNavigateUrlFields = new string[] { "Hyperlink" };
                    HLinkName.DataNavigateUrlFormatString = "{0}";
                    HLinkName.HeaderText = "Link Name";
                    HLinkName.Target = "_blank";
                    sgvLinks.Columns.Add(HLinkName);
                    sgvLinks.DataSource = getLinks();
                    sgvLinks.DataBind();
DataSet Method
public DataSet getLinks()
        {
            try
            {
                dtLinks.Columns.Add("Hyperlink");
                dtLinks.Columns.Add("HyperlinkDescription");
                string strHyperlink = string.Empty;
                string strHyperlinkDescription = string.Empty;
                SPWeb currentWeb = SPControl.GetContextWeb(Context);
                SPList LinksList = currentWeb.Lists["My Program Links"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                SPListItemCollection LinksListCollection = LinksList.GetItems(sQuery);
                if (LinksList != null)
                {
                    if (LinksListCollection.Count > 0)
                    {
                        foreach (SPListItem linkItem in LinksListCollection)
                        {
                            string strResultFromDataBase = string.Empty;
                            strResultFromDataBase = linkItem["URL"].ToString();
                            strHyperlink = strResultFromDataBase.Substring(0, strResultFromDataBase.IndexOf(','));
                            strHyperlinkDescription = strResultFromDataBase.Substring(strResultFromDataBase.IndexOf(',')+1);
                            DataRow drow = dtLinks.NewRow();
                            drow["Hyperlink"] = strHyperlink;
                            drow["HyperlinkDescription"] = strHyperlinkDescription;
                            dtLinks.Rows.Add(drow);
                        }
                       
                    }
                }
                dsetLinks.Tables.Clear();
                dsetLinks.Tables.Add(dtLinks);
                return dsetLinks;
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
                return null;
            }
        }

No comments:

Post a Comment