Friday, January 14, 2011

Get/View Audit Entries for Document Library in sharepoint 2010

SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite ElevatedSiteCollection = new SPSite("yoursite"))
            {
                using (SPWeb ElevatedSite = ElevatedSiteCollection.OpenWeb())
                {
                    Console.Write(ElevatedSite.Site.Url); SPList list = ElevatedSite.Lists["TestDocuments"];
                    // ElevatedSite.Lists[new Guid(ListId)];
                    Console.Write(list.Audit.AuditFlags + "\n");
                    SPListItem item = list.Items.GetItemById(list.Items[0].ID);
                    Console.Write(item.Title); SPAuditQuery wssQuery;
                    SPAuditEntryCollection auditCol; wssQuery = new SPAuditQuery(ElevatedSiteCollection);
                    wssQuery.RestrictToListItem(item); auditCol = ElevatedSite.Audit.GetEntries(wssQuery);
                    foreach (SPAuditEntry entry in auditCol)
                    {
                        // Query audit log with elevated privileges.
                        Console.Write(entry.Occurred.ToLocalTime() + " : " + entry.Event + " by " + GetUserNameById(entry.UserId, ElevatedSite) + "\n");
                    }
                }
            }
        });

Create a policy for Content type programmatically in Sharepoint 2010

using (SPSite siteCollection = new SPSite("suryasite")) { using (SPWeb web = siteCollection.OpenWeb()) { string strFeatureID = "Microsoft.Office.RecordsManagement.PolicyFeatures.PolicyAudit"; SPList list = web.Lists["Test2"]; SPContentType listctype = list.ContentTypes["CustomTemplate1"]; //listctype. // Microsoft.Office.RecordsManagement.PolicyFeatures.PolicyAudit Policy policyAudit = Policy.GetPolicy(listctype); if (policyAudit == null) // if (!policyAudit.Equals("CustomTemplate1")) { Policy.CreatePolicy(listctype, null); policyAudit = Policy.GetPolicy(listctype); policyAudit.Name = "OPS Audit Policy"; policyAudit.Statement = "OPS Audit on Project Documents content type"; //string.Format("Audits Edit/Delete/Restore {0} items", Helper.PREFIX); policyAudit.Description = policyAudit.Statement; if (policyAudit.Items[strFeatureID] == null) { string strCustomData = ""; policyAudit.Items.Add(strFeatureID, strCustomData); } } } }

Create/Get Documents from Document set programmatically in Sharepoint 2010

here i have one "Test Docs" library and it has some items which was created by using document set contenttype. when i click on on "Documentset item" it shows welcome page. i am trying to get documents which r in welcome page by using below code. SPFolder folder = SPContext.Current.ListItem.Folder; DocumentSet dset = DocumentSet.GetDocumentSet(folder); SPListItem listitem = dset.Item; foreach (SPFile item in listitem.Folder.Files) { } Create DOcument set programmatically in sharepoint 2010 SPList WorkingDocslist = web.Lists["TestDocs"]; SPFolder folderToInsertIn = WorkingDocslist.RootFolder; //Get the content type ID for the specified document set content type. SPContentTypeId dsCtId = new SPContentTypeId(); Hashtable properties = new Hashtable(); properties.Add("projectid", projectID); properties.Add("TaskID", taskID); //Create the new Document Set object. string strDocSetName = string.Empty; dsCtId = WorkingDocslist.ContentTypes["Project Negotiations Package"].Id; DocumentSet.Create(folderToInsertIn, strDocSetName , dsCtId, properties, true);

Followers