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

Saturday, October 1, 2016

Export SharePoint List to Excel

Export SharePoint List to Excel
go to ExportToExcel.ascx add below code.
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ExportToExcel.ascx.cs"Inherits="ExportToExcel.ExportToExcel.ExportToExcel" %>
<table>
    <tr id="trExportToExcel" runat="server">
        <td>
            <asp:Button ID="btnExcel" runat="server" Text="Export to Excel" OnClick="btnExcel_Click" />
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="lblError" runat="server"></asp:Label>
        </td>
    </tr>
</table>

go to ExportToExcel.ascx.cs add below code.
using Microsoft.SharePoint;
using System;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts; 
namespace ExportToExcel.ExportToExcel
{
    [ToolboxItemAttribute(false)]
    public partial class ExportToExcel : WebPart
    {
        public ExportToExcel()
        {
        } 
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        } 
        protected void Page_Load(object sender, EventArgs e)
        {
        } 
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            try
            {
                trExportToExcel.Visible = true;
                SPWeb oWeb = SPContext.Current.Web;
                SPBasePermissions perms = SPBasePermissions.ViewListItems;
                if (oWeb.DoesUserHavePermissions(SPBasePermissions.EnumeratePermissions))
                {
                    SPList oList = oWeb.Lists["test"];
                    SPUser oCurrentUser = oWeb.CurrentUser;
                    bool hasPermission = oList.DoesUserHavePermissions(oCurrentUser, perms);
                    if (hasPermission)
                    {
                        if (oList != null)
                        {
                            SPQuery qrylegalcases = new SPQuery();
                            qrylegalcases.Query = "<Where><IsNotNull><FieldRef Name='ID'/></IsNotNull></Where>";
                            SPListItemCollection itemslegalcases = oList.GetItems(qrylegalcases);
                            try
                            {
                                DataTable dt = new DataTable();
                                if (itemslegalcases.Count > 0)
                                {
                                    dt = itemslegalcases.GetDataTable();
                                }
                                ExportToExce(dt);
                            }
                            catch (Exception ex)
                            {
                                lblError.Text += " ## " + ex.Message + " # " + ex.StackTrace;
                            }
                        }
                        else
                        {
                            lblError.Text += "List deos not exist.";
                        }
                    }
                    else
                    {
                        lblError.Text += "You don't have permission to list.";
                    }
                }
                else
                {
                    lblError.Text += "You don't have permission to site.";
                }
            }
            catch (Exception ex)
            {
                lblError.Text += " ## " + ex.Message + " # " + ex.StackTrace;
            }
        } 
        private void ExportToExce(DataTable dt)
        {
            GridView GridView1 = new GridView();
            GridView1.AllowPaging = false;
            GridView1.DataSource = dt;
            GridView1.DataBind(); 
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.AddHeader("content-disposition""attachment;filename=ExportToExcel.xls");
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridView1.Rows[i].Attributes.Add("class""textmode");
            }
            GridView1.RenderControl(hw);
            string style = @"<style> .textmode { mso-number-format:\@; } </style>";
            HttpContext.Current.Response.Write(style);
            HttpContext.Current.Response.Output.Write(sw.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        } 
    }
}
deploy code and check.