|
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: - In the document library click on "Modify this View" for the view that you are attaching to this Query
- Scroll down to the "Folders" Section and click the plus sign to expand the area
- 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. - 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. - 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.
|