Geeks With Blogs

News
About Me:
I am a Female SharePoint Developer (a platinum unicorn). I have been working with SharePoint since I attended the Portal University in 2005. I hold a BA in Computer Science from the University of Missouri - Kansas City. I love playing Rockband, organizing user group meetings, working with code, attending events as a speaker or organizer, and having bizarre conversations about geek things with cool people. If you have any comments or questions fill out the contact form and I will try my best to help.



The Guide of a MOSSLover Becky Isserman's Blog

At work they wanted a global document library to upload Resumes that would link to users profiles.  For the first part I created a ListViewByQuery User Control, however you can do this in a WebPart incredibly easy.  What you want to do is add the ListViewByQuery in markup or create a new object like so:

Web User Control:
<SharePoint:ListViewByQuery ID="YourID" runat="server" />

private ListViewByQuery YourID; //add this where you would normally add global fields

WebPart:
protected ovverride void CreateChildControls()

{

ListViewByQuery YourID= new ListViewByQuery();

this.Controls.add(YourID);

}

Next you want to add a query and a list to the control somehow.  You can either use the PageLoad event or the RenderControl Event depending on if you are using a UserControl or a WebPart:

Web User Control:
private void Page_Load(Object sender, EventArgs e)

{

SPWeb Web = SPContext.Current.Web.ParentWeb.ParentWeb; //pass in the web where your library is located

SPList List = Web.Lists["YourList"]; //this is where you pass in your list name

SPQuery Query = new SPQuery(List.DefaultView); //this is where you pass in the view you want to show on the ListViewByQuery

Query.Query = "a CAML Query";  //may want to make life easier and use the U2U CAML Query Builder if you are not

Query.ViewFields = "<FieldRefName="Field1"/><FieldRefName="Field2" /> //these are the document library fields you want to show in the view

YourID.List = List; //this is where you want to add the list to your control

YourID.Query = Query; //this is where you want to add the query to your control

}
WebPart:

public override void RenderControl(HTMLTextWriter writer)

{

SPWeb Web = SPContext.Current.Web.ParentWeb.ParentWeb; //pass in the web where your library is located

SPList List = Web.Lists["YourList"]; //this is where you pass in your list name

SPQuery Query = new SPQuery(List.DefaultView); //this is where you pass in the view you want to show on the ListViewByQuery

Query.Query = "a CAML Query"; //may want to make life easier and use the U2U CAML Query Builder if you are not

Query.ViewFields = "<FieldRefName="Field1"/><FieldRefName="Field2" /> //these are the document library fields you want to show in the view

YourID.List = List; //this is where you want to add the list to your control

YourID.Query = Query; //this is where you want to add the query to your control

YourID.RenderControls(writer); //this is where you render the control in the webpart

}

Note: There is really only one line of code difference between what you add to the Page_Load in a web user control versus what you add to a RenderControl.  A WebPart may not be easier to use, but it may load faster.  It really depends on if you are using the SMARTPart versus if you were to add the control straight onto an ASP .Net Page and other factors.  Weigh the pros and cons and then decide which one you want to use.  Also, remember to play with your strengths and use what is the most comfortable for you.

Some Nuances...

No I did not forget about my title.  I had some issues when I was developing with this control, because we needed a folder for each two letter office code.  When you leave the folder in the view and you do a query using the FileLeafRef it shows the folder and not the items beneath the folder.  The folder has a nasty URL that does not work attached to the link.  Here is what I did to get around this issue:

  1. In the document library click on "Modify this View" for the view that you are attaching to this Query
  2. Scroll down to the "Folders" Section and click the plus sign to expand the area
  3. Click on the radio button next to the option that tells you "Do not show folders..."
    Note: Folders will no longer show in this view, which allows you to do a query based on the folder URL.  This part looks a little crazy, but it works.
  4. Go back into your code and create something that looks a little bit like so above your SPQuery:
    SPFolder Folder = Web.GetFolder(LibraryName+"/"+FolderName);
    This will get the folder you need.
  5. In the Query.Query do a little something like this:
    Query.Query = "<where><BeginsWith><FIeldRef Name='FileLeafRef'/><Value Type='Lookup'>"+Folder.Url"</Value></BeginsWith></where>";
    So if you look FileLeafRef is the full url for your file name, so it checks if the url has the folder url in the beginning and returns all the items with that folder url in the beginning.  I know it's a little nutty, but it does the job.

So that is basically how you would use an SPLIstByQuery Control and workaround the folder bug that I described.  If anyone has any questions I am more than happy to assist you if I can or I can try to find an answer with my mad searching skills.  I am hoping that sometime by the end of the week I will have some sample code on the next piece of this project.  I am working with a List Definition for the Document Library and some item event receivers, so that when super users upload a person's resume they can attach it to their MOSS Profile.  I would say I love this project more so than the past projects, but I say that every new project.  I'll catch you guys later.

Posted on Wednesday, July 16, 2008 6:48 PM MOSS | Back to top


Comments on this post: What I've Been Up To...ListViewByQuery and Its Nuances

# re: What I've Been Up To...ListViewByQuery and Its Nuances
Requesting Gravatar...
Yeah, the LVBQ control is seducing isn't it? Great for quick and dirty access to lists from in other webs or site collections.

It is a little nasty thing when you try to use it more than once on a page, say to display documents from one web and some tasks from another. The item menus are not properly configured, thus when you try to open an item from within one LVBQ instance it will always refer to the item with the same ID in the list that has been added first to the page.

In this example: if the doc lib is added first and then a task list (both as LVBQs of course) trying to open a task list item using the fancy item level menu will open the doc lib item with the same ID (certainly only if there exists such item). If you change the web part order it is exactly the other way round. Try it out.

I believe it's a bug, a nasty one for sure.

Regards
Left by Steve on Aug 10, 2008 9:39 AM

# re: What I've Been Up To...ListViewByQuery and Its Nuances
Requesting Gravatar...
Thanks for the info on the LVBQ. I too was frustrated by the problem Steve elaborated on - needing to have two+ instances of my custom web part (using LVBQ) on the same page.

My work around involved a couple of steps. First, add a property to the web part to indicate the "instance" of the webpart on the page. (I could probably have programmatically figured out how many instances of my web part already existed, but I wanted to get a soln finished).

This value is set to a unique value for the page (e.g. 1, 2, 3, etc.) Then in my webpart code, I output the LVBQ to html (via the list.RenderToHtml method). Finally, I perform a replace on the string to replace "ctx1" with "ctx" + myuniquenum and also replace CtxNum="1" with CtxNum=" + myuniquenum + ".

These changes have worked for me so far. If I get a chance to blog about my change further, I'll post the link.

Thanks again...
Left by Josh on Oct 28, 2008 10:48 AM

# re: What I've Been Up To...ListViewByQuery and Its Nuances
Requesting Gravatar...
Yeah you can use something called a ViewToolbar and set it to the list you want.
Left by MOSSLover on Nov 18, 2008 9:34 PM

# re: What I've Been Up To...ListViewByQuery and Its Nuances
Requesting Gravatar...
Do you know how I would remove the ".aspx" extension from showing when I use the following code in SPD using data view?

<xsl:value-of select="@FileLeafRef" />

I would much rather use my column named "Title" but I have no idea how I would link it to the correct page.

Thanks
Left by Tom A on Jan 05, 2009 2:59 PM

# re: What I've Been Up To...ListViewByQuery and Its Nuances
Requesting Gravatar...
Dim web As SPWeb = SPContext.Current.Web
Dim List As SPList =web.ListsDocuments") '//this is where you pass in your list name

Dim Query As SPQuery = New SPQuery(List.DefaultView) '//this is where you pass in the view you want to show on the ListViewByQuery

Dim objFolder As SPFolder = web.GetFolder(clsPIConstants.DCN_OPP_FOLDER_PATH & "Babu")

Dim FolderURL As String = objFolder.Url

If FolderURL.StartsWith("/") Then
FolderURL = FolderURL.Substring(1)
End If


Query.RowLimit = 10

Query.Query = " <Where> <BeginsWith> <FieldRef Name='FileRef' /> <Value Type='Lookup'>" & FolderURL & "</Value> </BeginsWith></Where>"

Query.ViewAttributes = "BaseViewID=""1"" Scope=\""Recursive"""

ListViewByQuery1.List = List
ListViewByQuery1.Query = Query
ListViewByQuery1.DataBind()
Left by Surendrababu on Feb 09, 2009 1:06 AM

# re: What I've Been Up To...ListViewByQuery and Its Nuances
Requesting Gravatar...
I am working with the ListViewByQuery control and it is working great. I am passing in the list and the view I need. I can't seem to get it to display a calendar view though. It displays all other views fine including All Events and Current Events but nothing for Calendar. If anyone else has experienced this and fixed let me know.
Left by DCP on Aug 10, 2009 9:32 AM

# re: ctx replacement
Requesting Gravatar...
Hi. Just wanted to give heads-up on the ctx replacement technique: when you sort your ListViewByQuery webpart on some of the fields, the javascript therein rebuilds the ctx object, not ctx# you defined by replacing it. Make sure you assign ctx#=ctx after sorting and filtering
Left by AKrasheninnikov on May 27, 2010 2:11 AM

# DataSheet View Support
Requesting Gravatar...
Hi, I have been trying to use this the ListViewByQuery control on a subsite to display a list from the parent site. It seems to support standard list views fine, however I cannot get the DataSheet view to display on the child site. Is the DataSheet View support for this.

Thanks,Moni
mattsingh08@gmail.com
Left by Moninder on Dec 30, 2010 6:36 PM

# re: What I've Been Up To...ListViewByQuery and Its Nuances
Requesting Gravatar...
Hi Becky!
I'm developed a web part, and wanted to try to work with ListViewByQuery. In the marcup-code of ascx control, I'm added
<SharePoint:ListViewByQuery ID="CustomersListViewByQuery" runat="server"/>

and in Page_Load method next:
SPList list = SPContext.Current.Web.Lists["Customers"];
CustomersListViewByQuery.List = list;

as a result of the deployment and adding Web Part to page. I'm got the error page displayed in the browser:

[NullReferenceException: Object reference not set to an instance of an object.]
Microsoft.SharePoint.WebControls.ListViewByQuery.R­ender(HtmlTextWriter writer) +586
System.Web.UI.Control.RenderChildrenInternal(HtmlT­extWriter writer, ICollection children) +245
System.Web.UI.HtmlControls.HtmlForm.RenderChildren­(HtmlTextWriter writer) +255
System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTex­tWriter output) +323
System.Web.UI.HtmlControls.HtmlForm.RenderControl(­HtmlTextWriter writer) +72
System.Web.UI.Control.RenderChildrenInternal(HtmlT­extWriter writer, ICollection children) +245
System.Web.UI.Page.Render(HtmlTextWriter writer) +37
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4427
Left by damir on Feb 28, 2012 12:49 AM

Your comment:
 (will show your gravatar)


Copyright © MOSSLover | Powered by: GeeksWithBlogs.net