Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Here is some code I've been playing with to display all items that have been changed in a web and all subwebs in a visual webpart (it grabs a heap of info and chucks it in a label):

    protected void Page_Load(object sender, EventArgs e)
    {
        String ChangeLog = "";

        SPChangeQuery query = new SPChangeQuery(false,false);
        query.Add = true; query.Update = true; query.Delete = true;
        query.File = true; query.Item = true;

        SPChangeCollection CurrentSiteChanges = SPContext.Current.Site.GetChanges(query);

        foreach(SPChange Change in CurrentSiteChanges)
        {
            if (Change.GetType() == typeof(SPChangeItem))
            {
                SPChangeItem ChangeItem = Change as SPChangeItem;
                using (SPWeb Web = SPContext.Current.Site.OpenWeb(ChangeItem.WebId))
                {
                    foreach (SPWeb ChildWeb in SPContext.Current.Web.GetSubwebsForCurrentUser())
                    {
                        SPFile Item = Web.GetFile(ChangeItem.UniqueId);
                        if (Item.Exists)
                        {
                            /*
                            ChangeLog += "File: <a href='" + Item.Url + "'>" + Item.Name + "</a> @ " + ChangeItem.Time.ToString() + " | Site: " + Web.Title + "<br />";
                             */
                        }
                    }
                }
            }
        }
        lbl_Webs.Text = ChangeLog;
    }

I want to know how I can place the info in the commented section of the code above into a control that displays items like other webparts usually do (don't know what this control will be, a ListView or something). Can anybody advise how to do this, or suggest an alternative if I'm doing this completely wrong?

share|improve this question

1 Answer 1

You can use DataTable and SPGridView combo.

Define the SPGridView in the markup like below:-

<SharePoint:SPGridView ID="spgvChangeLists" runat="server" AutoGenerateColumns="False">
<Columns>
    <SharePoint:SPBoundField DataField="Site" HeaderText="Site" />
    <SharePoint:SPBoundField DataField="TimeChange" HeaderText="Change Time" />
    <asp:TemplateField HeaderText="File">
        <ItemTemplate>
            <asp:HyperLink ID="hlFileLink" runat="server" Text='<%# Eval("ItemName") %>' NavigateUrl='<%# Eval("ItemUrl")%>' />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
<EmptyDataTemplate>
    <strong>There are no changes/strong>
</EmptyDataTemplate>

And in the code behind define the DataTable, get the changes data and bind it to the spgridview like below:-

 protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ItemName");
        dt.Columns.Add("ItemUrl");
        dt.Columns.Add("TimeChange");
        dt.Columns.Add("Site");

        SPChangeQuery query = new SPChangeQuery(false, false);
        query.Add = true; query.Update = true; query.Delete = true;
        query.File = true; query.Item = true;

        SPChangeCollection CurrentSiteChanges = SPContext.Current.Site.GetChanges(query);

        foreach (SPChange Change in CurrentSiteChanges)
        {
            if (Change.GetType() == typeof(SPChangeItem))
            {
                SPChangeItem ChangeItem = Change as SPChangeItem;
                using (SPWeb Web = SPContext.Current.Site.OpenWeb(ChangeItem.WebId))
                {
                    foreach (SPWeb ChildWeb in SPContext.Current.Web.GetSubwebsForCurrentUser())
                    {
                        SPFile Item = Web.GetFile(ChangeItem.UniqueId);
                        if (Item.Exists)
                        {
                            DataRow dr = dt.NewRow();

                            dr["ItemUrl"] = Item.Url;
                            dr["ItemName"] = Item.Name;
                            dr["TimeChange"] = ChangeItem.Time.ToString();
                            dr["Site"] = Web.Title;

                            dt.Rows.Add(dr);
                        }
                    }
                }
            }
        }

        spgvChangeLists.DataSource = dt;
        spgvChangeLists.DataBind();
    }

If you want sorting, paging and filtering functionality without writing any code, you can use ObjectDataSource + DataTable + SPGridView or LinqDataSource + Custom Objects + SPGridView Hope this helps

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.