Search This Blog

Monday, November 7, 2016

Get One form Library Item to Another Form Using Rest API



In Web Config :

<appSettings>

<add key="QualityBlogServiceUrl" value="http://projects.Ngo-infotech.com/sites/cmmicouncils/blogs/_api/web/Lists/getbytitle('Posts')/items" />
<add key="QualityBlogPostUrl" value="http://projects.Ngo-infotech.com/sites/cmmicouncils/blogs/Lists/Posts/Post.aspx?ID=" />
<add key="QualityBlogNewPost" value="http://projects.Ngo-infotech.com/sites/cmmicouncils/blogs/Lists/Posts/NewPost.aspx" />
</appSettings>

HTML Code
<TABLE border=0 cellSpacing=0 cellPadding=0 width="85%" align=center>
<TBODY>
<TR>
<TD style="height6px"><IMG alt="" src="SiteAssets/NGO_Intranet/images/left-cor.gif" width=6 height=6></TD>
<TD background=SiteAssets/NGO_Intranet/images/top-cen.gif width="100%" style="height6px"><IMG alt="" src="SiteAssets/NGO_Intranet/images/top-cen.gif" width=2 height=6></TD>
<TD style="height6px"><IMG alt="" src="SiteAssets/NGO_Intranet/images/rt-cor.gif" width=6 height=6></TD></TR>
<TR>
<TD vAlign=top background=SiteAssets/NGO_Intranet/images/lft-img.gif><IMG alt="" src="SiteAssets/NGO_Intranet/images/lft-img.gif" width=6 height=2></TD>
<TD vAlign=top>
<TABLE border=0 cellSpacing=0 cellPadding=0 width=170>
<TBODY>
<TR>
<TD style="vertical-align:top;">
<div style="margin-top:23px;"><a href="http://projects.NGO-infotech.com/sites/cmmicouncils/blogs/" target="_blank"><IMG  alt="CMMI" src="SiteAssets/NGO_Intranet/images/cmmi-desk.png"></a></div>
      
</TD>
<TD align=middle>
<asp:Label runat="server" Text="" ID="lblXYz"></asp:Label>
</TD>

</TR>
</TBODY></TABLE>

</TD>
<TD vAlign=top background=SiteAssets/NGO_Intranet/images/rt-img.gif><IMG alt="" src="SiteAssets/NGO_Intranet/images/rt-img.gif" width=6 height=3></TD></TR>
<TR>
<TD><IMG alt="" src="SiteAssets/NGO_Intranet/images/left-cor1.gif" width=6 height=6></TD>
<TD background=SiteAssets/NGO_Intranet/images/bttm-cen.gif><IMG alt="" src="SiteAssets/NGO_Intranet/images/bttm-cen.gif" width=3 height=6></TD>
<TD><IMG alt="" src="SiteAssets/NGO_Intranet/images/rt-cor1.gif" width=6 height=6></TD></TR></TBODY></TABLE>



Code:


using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Text;

namespace QualityPostSolution.QualityPostvwp
{
    [ToolboxItemAttribute(false)]
    public partial class QualityPostvwp : WebPart
    {
        public QualityPostvwp()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {  
            try
            {
                GetAllSPListItems(); 
            }
            catch
            {

            }

        }
        public  void GetAllSPListItems()
        {
            try
            {
                string qualityBlogServiceUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["QualityBlogServiceUrl"].ToString();
                string QualityBlogNewPost = System.Web.Configuration.WebConfigurationManager.AppSettings["QualityBlogNewPost"].ToString();
                string QualityBlogPostUrl = System.Web.Configuration.WebConfigurationManager.AppSettings["QualityBlogPostUrl"].ToString();

                string jsonRequest = qualityBlogServiceUrl;
                HttpWebRequest spRequest =
                   (HttpWebRequest)HttpWebRequest.Create(jsonRequest);
                spRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
                spRequest.Credentials = new System.Net.NetworkCredential("ngo""ngo1234""NGO");
                spRequest.Method = "GET";
                spRequest.Accept = "application/json;odata=verbose";
                spRequest.ContentType = "application/json;odata=verbose";
                WebResponse endpointResponse = spRequest.GetResponse();
               // HttpWebResponse endpointResponse =(HttpWebResponse)spRequest.GetResponse();
                Data data = null;
                using (endpointResponse)
                {
                    using (Stream respStream = endpointResponse.GetResponseStream())
                    {
                        var reader = new StreamReader(respStream, Encoding.UTF8);
                        string strred = reader.ReadToEnd();
                        JavaScriptSerializer j = new JavaScriptSerializer();
                        data = j.Deserialize<Data>(strred);
                        string vQualityMarHtml = "";
                        vQualityMarHtml += "<div style=\"margin-bottom:5px;display:block;height:20px;margin-left:-7px;\">";
                        vQualityMarHtml += "<a target=\"_blank\" href=" + QualityBlogNewPost + "><img src=\"SiteAssets/NGO_Intranet/images/create-new-blog.png\"/></a>";
                        vQualityMarHtml += "</div>";
                        vQualityMarHtml += "<marquee style=\"height:82px;padding-left:5px;\" scrollamount=\"2\" direction=\"up\" onmouseover=\"this.stop();\" onmouseout=\"this.start();\">";
                        foreach (SharePointListItem post in data.d.results)
                        {
                            vQualityMarHtml += "<div style=\"margin-bottom:5px;\">";
                            vQualityMarHtml += "<a style=\"font-family:Verdana, Arial, Helvetica, sans-serif;font-size:10px;color:#003399;\" target=\"_blank\" href=" + QualityBlogPostUrl + post.id + ">" + post.Title + "</a>";
                            vQualityMarHtml += "</div>";
                        }
                        vQualityMarHtml += "</marquee>";
                        lblXyx.Text = vQualityMarHtml;

                    }
                }


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
      
    }
    public class Data
    {
        public Results d { getset; }
    }

    public class Results
    {
        public SharePointListItem[] results { getset; }
    }

    public class SharePointListItem
    {
        public string id { getset; }
        public string Title { getset; }
    }
}