简体   繁体   English

Sharepoint 2010中的自定义AnnouncementListItem可视Web部件

[英]Custom AnnouncementListItem Visual Web Part in Sharepoint 2010

I am trying to create a visual web Part to display the latest 5 announcement list items. 我正在尝试创建一个可视化Web部件以显示最新的5条公告列表项。

I need the Announcement List Item Title to show up as a link and any attachments(only pictures) to be displayed right above it. 我需要将公告列表项目标题显示为链接,并将所有附件(仅图片)显示在其上方。 I am planning on refreshing the web part every 15-20 minutes to be able to show the latest announcement. 我计划每15-20分钟刷新一次Web部件,以显示最新公告。

I dont know how and what the best asp control and page design would be to display these items. 我不知道如何以及什么是最好的ASP控件和页面设计来显示这些项目。

Here is the CAML Query with the rest of the code I have: 这是CAML查询以及其余的代码:

 using (SPSite oSPsite = new SPSite("http://mySharePointWebApp:Port#/"))
         {

            using (SPWeb oSPWeb = oSPsite.OpenWeb())
             {
                 oSPWeb.AllowUnsafeUpdates = true;

                // Fetch the List
                 SPList list = oSPWeb.Lists["Announcements"];

                SPQuery spQuery = new SPQuery();
                 //spQuery.Query = "<Where> <Eq> <FieldRef Name='Title' /> <Value           Type='Text'></Value> </Eq> </Where>";
                 spQuery.Query = "";
                 spQuery.RowLimit = 5;

                // Show item in text box
                 SPListItemCollection oListCollection = list.GetItems(spQuery);
                 foreach (ListItem oListItem in oListCollection)
                 {
                     // **What should I go with here?**
                 }

            }
         }

You can use a repeater control like listbox and customize it using item templates. 您可以使用列表框等中继器控件,并使用项目模板对其进行自定义。

<asp:ListBox>
    <item template>
       <div>
         <image control/>
         <text control/>
       </div>
    </item template>
</asp:ListBox>

To get the latest 5 announcements, write CAML SPQuery to get top 5 items by ID in descending order. 要获取最新的5条公告,请编写CAML SPQuery以按ID降序获取前5条。 Check to see if an attachment exist for the announcement. 检查公告是否存在附件。 If it does then get the attachment URL and check if it is an image type by looking at the extension. 如果确实存在,则获取附件URL,并通过查看扩展名检查它是否为图像类型。

If image exist then assign the attachment relative url to the image control in the item template. 如果存在图像,则将附件相对URL分配给项目模板中的图像控件。 For announcements with no image you can choose to either hide the image control or assign URL to some common image. 对于没有图像的公告,您可以选择隐藏图像控件或将URL分配给某些公共图像。

You can also use Linq to SharePoint to get the latest 5 announcements. 您还可以使用Linq to SharePoint获得最新的5条公告。 Code should look like this 代码应如下所示

var top5Announcements = (From a in siteDataContext.Announcements OrderBy a.id descending select a).Take(5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM