views:

493

answers:

1

Hello everyone,

I am using SharePiont Server 2007 Enterprise with Windows Server 2008 Enterprise, and I am using publishing portal template. I am developing using VSTS 2008 + C# + .Net 3.5. I have defined a custom list manully on a SharePoint site (all column types of the custom list are SharePoint built-in types), and I want to define some customized rules to filter this list to display only a part of the list. Any reference code?

EDIT1: Here is my current code. I have used such code to retrieved the items I need, but how to display the retieved items in a SharePoint list?

                SPSite oSiteCollection = SPContext.Current.Site;
                SPList oList = oSiteCollection.AllWebs[0].Lists["PeopleTest"];
                SPQuery oQuery = new SPQuery();
                oQuery.Query = "<Where><Eq><FieldRef Name='Department'/>" +
                        "<Value Type='Text'>Computer</Value></Eq></Where>";
                SPListItemCollection collListItems = oList.GetItems(oQuery);

                foreach (SPListItem oListItem in collListItems)
                {
                    writer.Write(oListItem["Department"].ToString()+"###");
                }

thanks in advance, George

+2  A: 

If you're using server (i.e., not client) dlls, you can use SPList.GetItems Method with SPQuery as a parameter:

SPSite oSiteCollection = SPContext.Current.Site;
SPList oList = oSiteCollection.AllWebs["Site_Name"].Lists["List_Name"];
SPQuery oQuery = new SPQuery();
oQuery.Query = "<Where><Eq><FieldRef Name='Schedule'/>" +
        "<Value Type='CHOICE'>2 weeks</Value></Eq></Where>";
SPListItemCollection collListItems = oList.GetItems(oQuery);

foreach (SPListItem oListItem in collListItems)
{
    Label1.Text += SPEncode.HtmlEncode(oListItem["Title"].ToString()) 
        + " -- " + SPEncode.HtmlEncode(oListItem["EndDate"].ToString())   
        + "<BR>";
}

Note that foreach part of this example code which displays data in HTML format, is provided only for example. You may use the same rendering as you use at present. The key is to use SPQuery and .GetItems(...).

If you use client dlls, see this link and others from MSDN (this link is only an example, it is relevant for SharePoint Team Services Client API).

Roman Boiko
Thanks! I should develop a web part based on your code? Or using your code inside an existing list?
George2
Server DLL you mean Microsoft.SharePoint.dll which is shipped with SharePoint Server 2007 SDK?
George2
You can develop based on web parts or create a library project with functionality you need, and call it from you code of any type. You may even create a console application and run it at SharePoint server...
Roman Boiko
Yes, Microsoft.SharePoint.dll for server-side API, and Microsoft.SharePoint.Client.dll for client-side. And there are other dlls referenced in MSDN.
Roman Boiko
Thanks Roman! I tried the code and it filters the list and display in html format. What I need is to filter the list and display in the same SharePoint list rendering style. Any reference code?
George2
I updated the answer. You should investigate the way it is displayed at present in your case, and use the same way, only change your code to call `.GetItems(SPQuery)`
Roman Boiko
I have tried, but how to display the retieved items in a SharePoint list?
George2
I have posted my code in EDIT1 section of my post. Any solutions to display the retieved items in a SharePoint list?
George2
You don't need programming for that. Just create a View page and configure filtering on it. This can be done by any user (i.e., not a programmer) with administrator's permissions.
Roman Boiko
Thanks, question answered!
George2
You're welcome :)
Roman Boiko