Thursday, March 7, 2013

SharePoint ListView by Query in Web part with Login User


        Using ListView by Query in webpart 




Scenario:
I wanted to display information into grid using a CAML query

Solution:
Share Point has a ListViewByQuery Control which can help.

Code:
   



     
  HyperLink lnkHyper;
        protected override void CreateChildControls()
        {
            SPWeb currentweb = SPContext.Current.Web;
            SPList list = currentweb.Lists["RKUCustList"];
            if (list != null)
            {
                lnkHyper = new HyperLink();
                lnkHyper.Text = "CREAT (+)";
                lnkHyper.NavigateUrl = "javascript:SP.UI.ModalDialog.ShowPopupDialog('" + list.Forms[PAGETYPE.PAGE_NEWFORM].ServerRelativeUrl + "')";

            }
        }
        public override void RenderControl(HtmlTextWriter writer)
        {
            Controls.Add(lnkHyper);
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                ListViewByQuery view = new ListViewByQuery();
                SPWeb currentweb = SPContext.Current.Web;
                SPList list = currentweb.Lists["RKUCustList"];

                view.List = list; SPQuery query = new SPQuery(view.List.DefaultView);
                query.ViewFields = string.Concat(
                                                  "<FieldRef Name='Title' />",
                                                  "<FieldRef Name='Fname' />",
                                                  "<FieldRef Name='Lname' />",
                                                  "<FieldRef Name='Attachments'/>",
                                                   "<FieldRef Name='Type'/>",
                                                  "<FieldRef Name='Edit'/>",
                                                  "<FieldRef Name=" + lnkHyper + "/>");
                Controls.Add(lnkHyper);

                query.Query = @"<Where>   <And>   <IsNotNull>  <FieldRef Name='Fname' />
         </IsNotNull> <Eq> <FieldRef Name='Status' /> <Value Type='Choice'>Save</Value> </Eq> </And> </Where>";
                view.Query = query;
                view.DisableFilter = true;
                EnsureChildControls();
                view.RenderControl(writer);
                RenderChildren(writer);

            });
        }






||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||@@@@@@@@@|||||||||||||||||||||||||||||||||||||||||

public override void RenderControl(HtmlTextWriter writer)
{
  SPSecurity.RunWithElevatedPrivileges(delegate()
   {    ListViewByQuery view = new ListViewByQuery();
    SPWeb currentweb = SPContext.Current.Web;
    SPList list = currentweb.Lists["CAMLList"];
   
    string uName = string.Empty;

    if (currentweb.CurrentUser.LoginName != null &&
        !String.IsNullOrEmpty(currentweb.CurrentUser.LoginName.ToString()))
      {
          uName = currentweb.CurrentUser.LoginName;
      }
   else
     {
      uName = System.Web.
      HttpContext.Current.User.Identity.Name;
     }

    view.List = list;
    SPQuery query = new SPQuery(view.List.DefaultView);
    query.ViewFields =  string.Concat(
                                       "<FieldRef Name='Title' />",
                                       "<FieldRef Name='Name' />",
                                       "<FieldRef Name='EId' />");

    query.Query = "<Where><And><IsNotNull><FieldRef Name='Title' /></IsNotNull><Contains><FieldRef Name='Country' /><Value Type='Text'>" + uName + "</Value></Contains></And></Where>";
   view.Query = query;
   view.DisableFilter = true;

   EnsureChildControls();
   view.RenderControl(writer);
   RenderChildren(writer);  


   });
}


No comments:

Post a Comment