Thursday, 5 December 2013

Get value from Document Library in sharepoint 2013

Get value from Document Library in sharepoint 2013

<script type="text/javascript">
    function OpenDialogSubReport(URL, Title) {

        var NewPopUp = SP.UI.$create_DialogOptions();
        NewPopUp.url = URL;
        NewPopUp.autoSize = true;
        NewPopUp.title = Title;
        NewPopUp.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
        SP.UI.ModalDialog.showModalDialog(NewPopUp);
        return false;
    }
</script>



<asp:GridView ID="gvDocuments" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
    <Columns>
       <%-- ItemStyle-CssClass="hide" ControlStyle-CssClass="hide" HeaderStyle-CssClass="hide"--%>
        <asp:BoundField HeaderText="ID" DataField="ID"  />
        <asp:BoundField HeaderText="Type" DataField="DocIcon" />
        <asp:HyperLinkField HeaderText="Name" DataTextField="LinkFilename" DataNavigateUrlFields="EncodedAbsUrl" DataNavigateUrlFormatString="{0}" Target="_blank" />
        <asp:BoundField HeaderText="Modified" DataField="Modified" />
        <asp:BoundField HeaderText="Modified By" DataField="Editor" />
        <asp:TemplateField ItemStyle-Width="70px" ItemStyle-CssClass="EditIcon MlstEdit">
            <ItemTemplate>
                <%--<a id="Share"  href="javascript:void(0);" onclick="OpenPopUpPage('/_layouts/15/ShareDoc.aspx?forSharing=1&List={91A30FF8-FBF1-4DC3-ABE4-817507EEA64E}&obj={91A30FF8-FBF1-4DC3-ABE4-817507EEA64E},<%# Eval("ID") %>,DOCUMENT');">Share</a>--%>
                 <%--<a id="Share"  href="javascript:void(0);" onclick="OpenPopUpPage('http://sharepoint:8888/Pages/Shared.aspx');">Share</a>--%>
                <a id="Share"  href="javascript:void(0);" onclick='<%# Eval("ID", "OpenPopUpPage(\"/Pages/Shared.aspx?ID={0}\")") %>'>Share</a>
           
                <%--<asp:LinkButton ID="lnkShare" runat="server" OnClick="lnkShare_Click" Text="Share"></asp:LinkButton>--%>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
    <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
    <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
    <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#FFF1D4" />
    <SortedAscendingHeaderStyle BackColor="#B95C30" />
    <SortedDescendingCellStyle BackColor="#F1E5CE" />
    <SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>




 protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    this.BindGridview();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void BindGridview()
        {
            try
            {
                SPSite oSite = null;
                SPWeb oWeb = null;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    oSite = SPContext.Current.Site;
                    using (oSite = new SPSite(oSite.Url))
                    {
                        using (oWeb = oSite.OpenWeb())
                        {
                            SPDocumentLibrary lst = (SPDocumentLibrary)oWeb.Lists.TryGetList("Documents");
                            if (lst != null)
                            {
                                SPQuery sQuery = new SPQuery();
                                sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                                sQuery.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='DocIcon' /><FieldRef Name='Editor' /><FieldRef Name='EncodedAbsUrl' /><FieldRef Name='LinkFilename' /> <FieldRef Name='FileLeafRef' /><FieldRef Name='Modified' />";                                
                                sQuery.ViewAttributes = "Scope='Recursive'";
                                sQuery.ViewFieldsOnly = true;
                                SPListItemCollection myColl = lst.GetItems(sQuery);
                                if (myColl.Count > 0)
                                {
                                    gvDocuments.DataSource = myColl.GetDataTable();
                                    gvDocuments.DataBind();
                                }
                            }
                        }

                    }

                });
            }


            catch (Exception)
            {

                throw;
            }

        }

        protected void lnkShare_Click(object sender, EventArgs e)
        {

            string SiteUrl = SPContext.Current.Site.Url;            
            string url = SiteUrl + "/_layouts/15/ShareDoc.aspx?forSharing=1&List={91A30FF8-FBF1-4DC3-ABE4-817507EEA64E}&obj={91A30FF8-FBF1-4DC3-ABE4-817507EEA64E},2,DOCUMENT";
            HttpContext.Current.Response.Redirect(url);
           //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), Guid.NewGuid().ToString(), "javascript:OpenDialogSubReport('" + url + "','" + Title + "')", true);
        }


No comments:

Post a Comment