none
How to Display current logged user document audits only

    Dotaz

  • Hi,

    For getting particular document library events i am using below code

    -----------------

    private void GetAudEntries()
            {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite mySite = new SPSite(SPContext.Current.Site.ID, SPContext.Current.Site.Zone))
                        {
                            using (SPWeb myWeb = mySite.RootWeb)
                            {
                                SPList myList = myWeb.Lists["PublicUserScripts"];
                                SPListItemCollection totalitems = myList.Items;
                               // int totallistitems = totalitems.Count;
                                DataTable myDataTable = new DataTable();
                                DataRow myRow;
                                SPUser user;
                                try
                                {
                                    //SPListItem myDoc = totalitems[];
                                    SPAuditQuery wssQuery = new SPAuditQuery(mySite);
                                    wssQuery.RestrictToList(myList);
                                    SPAuditEntryCollection auditCol = myList.Audit.GetEntries(wssQuery);

                                    myDataTable.Columns.Add("Event");
                                    myDataTable.Columns.Add("ItemType");
                                    myDataTable.Columns.Add("DocLocation");
                                    myDataTable.Columns.Add("Occurred", typeof(DateTime));
                                    myDataTable.Columns.Add("UserName");

                                    foreach (SPAuditEntry entry in auditCol)
                                    {
                                        if (entry.DocLocation.EndsWith(".xls") || (entry.DocLocation.EndsWith(".doc")) || (entry.DocLocation.EndsWith(".ppt")) || (entry.DocLocation.EndsWith(".txt")) || (entry.DocLocation.EndsWith(".docx")))
                                        {
                                            user = myWeb.SiteUsers.GetByID(entry.UserId);
                                            myRow = myDataTable.NewRow();
                                            myRow["Event"] = entry.Event;
                                            myRow["ItemType"] = entry.ItemType.ToString();
                                            myRow["DocLocation"] = entry.DocLocation;
                                            myRow["Occurred"] = entry.Occurred.ToLocalTime();
                                            myRow["UserName"] = user.ToString();
                                            myDataTable.Rows.Add(myRow);
                                        }
                                    }
                                    myGridView.DataSource = myDataTable;
                                    myGridView.DataBind();
                                }
                                catch (Exception ex)
                                {
                                    errorMessage = ex.ToString();
                                }
                            }
                        }
                });

            }

    ---------

    Here i am able see all list events that’s fine.

    But i want to display the current logged user documents events only.

    Example: if i have two users named with User1,User 2 and User3
    If User1 logged into site, then his document(which are uploaded by current user(User 1) only) audits only should display to him which are viewed or updated by another Users (User2,User3).
    I hope you people get my need
    Please help me


    MD.Liakath ali


    • Upravený Liakath 13. června 2012 12:55
    13. června 2012 12:46

Odpovědi

  • I'm sorry, I mis-understood your requirement in your first post.  To be clear, you're trying to show all audit logs only for items that were created by the currently logged in user, right?

    If that's the case, something like the following should work.

    SPQuery query = new SPQuery();
    query.Query = "<Where><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Integer'>" + userId + "</Value></Eq></Where>";
    
    foreach (SPListItem item in myList.GetItems(query))
    {
        SPAuditQuery wssQuery = new SPAuditQuery(mySite);
        wssQuery.RestrictToListItem(item);
        SPAuditEntryCollection auditCol = myList.Audit.GetEntries(wssQuery);
    
        foreach (SPAuditEntry entry in auditCol)
        {
            // Add to data table...
        }
    }

    This will fetch all of the items that were created by the current user in the list, and then perform an audit query on each item.  This is probably going to be a little slow, but I don't see another way around that.

    You can also filter for certain files in the SPQuery.  See here for an example.

    Hope that helps!


    Bob Guidinger
    Please remember to click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    • Navržen jako odpověď Ravi S Kulkarni 14. června 2012 16:01
    • Označen jako odpověď Liakath 17. června 2012 15:41
    14. června 2012 15:09

Všechny reakce

  • You can use SPAuditQuery.RestrictToUser to do this.  Change your code so it looks like this.

    int userId = SPContext.Current.Web.CurrentUser.ID;
    
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        ...
    
        SPAuditQuery wssQuery = new SPAuditQuery(mySite);
        wssQuery.RestrictToList(myList);
        wssQuery.RestrictToUser(userId);
    
        ...
    }


    Bob Guidinger
    Please remember to click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    13. června 2012 14:02
  • Hi Bob,

    thanks for your reply,

    I used below code

    private void GetAudEntries()
            {
                int userId = SPContext.Current.Web.CurrentUser.ID;
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite mySite = new SPSite(SPContext.Current.Site.ID, SPContext.Current.Site.Zone))
                        {
                            using (SPWeb myWeb = mySite.RootWeb)
                            {
                                SPList myList = myWeb.Lists["PublicUserScripts"];
                                SPListItemCollection totalitems = myList.Items;
                               // int totallistitems = totalitems.Count;
                                DataTable myDataTable = new DataTable();
                                DataRow myRow;
                                SPUser user;
                                try
                                {
                                    //SPListItem myDoc = totalitems[];
                                    SPAuditQuery wssQuery = new SPAuditQuery(mySite);
                                    wssQuery.RestrictToList(myList);
                                    wssQuery.RestrictToUser(userId);
                                    SPAuditEntryCollection auditCol = myList.Audit.GetEntries(wssQuery);

    --=-

    --

    }

    for above code my output is:

    here current logged user events are displaying that's fine, but here i am not able to see remaining users which are they viewed this user document.(other users are also viewed this user documents)

    can you please once again look into my pose above.


    MD.Liakath ali

    14. června 2012 5:35
  • I'm sorry, I mis-understood your requirement in your first post.  To be clear, you're trying to show all audit logs only for items that were created by the currently logged in user, right?

    If that's the case, something like the following should work.

    SPQuery query = new SPQuery();
    query.Query = "<Where><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Integer'>" + userId + "</Value></Eq></Where>";
    
    foreach (SPListItem item in myList.GetItems(query))
    {
        SPAuditQuery wssQuery = new SPAuditQuery(mySite);
        wssQuery.RestrictToListItem(item);
        SPAuditEntryCollection auditCol = myList.Audit.GetEntries(wssQuery);
    
        foreach (SPAuditEntry entry in auditCol)
        {
            // Add to data table...
        }
    }

    This will fetch all of the items that were created by the current user in the list, and then perform an audit query on each item.  This is probably going to be a little slow, but I don't see another way around that.

    You can also filter for certain files in the SPQuery.  See here for an example.

    Hope that helps!


    Bob Guidinger
    Please remember to click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    • Navržen jako odpověď Ravi S Kulkarni 14. června 2012 16:01
    • Označen jako odpověď Liakath 17. června 2012 15:41
    14. června 2012 15:09
  • Thank you very much for your code,

    this is what i need, i got the right output.


    MD.Liakath ali

    17. června 2012 15:43