Thursday, May 12, 2011

Sharepoint users who have not viewed documents in a SharePoint document library

        public string[] GetAllSharePointPeople()
        {
            SPSite Site = new SPSite("http://crm-srv:6677/");
            SPUserCollection AllUsers = Site.RootWeb.AllUsers;
            string strUserName = string.Empty;
            foreach (SPUser user in AllUsers)
            {
                if (strUserName == string.Empty)
                {
                    strUserName = user.Name.ToString();
                }
                else
                {
                    strUserName += "/" + user.Name.ToString();
                }
            }
            return strUserName.Split('/');
        }
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dtTable = new DataTable();
            dtTable.Columns.Add("DocumentName");
            dtTable.Columns.Add("UnViewedPeopleName");
            string[] arrAllUsers = GetAllSharePointPeople().Distinct().ToArray();
            SPSite oSiteCollection = new SPSite("http://crm-srv:6677/");
            SPWebCollection collWebsites = oSiteCollection.AllWebs;
            foreach (SPWeb oWebsite in collWebsites)
            {
                SPListCollection collLists = oWebsite.Lists;
                foreach (SPList oList in collLists)
                {
                    if (oList.BaseType == SPBaseType.DocumentLibrary)
                    {
                        SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)oList;
                        if (oList.BaseTemplate == SPListTemplateType.DocumentLibrary)
                        {
                            SPListItemCollection collListItems = oDocumentLibrary.Items;
                            string viewedUserName = string.Empty;
                            foreach (SPListItem oListItem in collListItems)
                            {
                                if (oList.Title != "Site Collection Images" &&
                                    oList.Title != "Images" &&
                                    oList.Title != "Style Library" &&
                                    oList.Title != "Reporting Templates" &&
                                    oList.Title != "Application Pages")
                                {
                                    string strWebSiteName = oWebsite.Name;
                                    string strListTitle = oList.Title;
                                    string strListItemName = oListItem.Name;
                                    SPAuditQuery wssQuery;
                                    SPAuditEntryCollection auditCol;
                                    wssQuery = new SPAuditQuery(oSiteCollection);
                                    wssQuery.RestrictToListItem(oListItem);
                                    auditCol = oWebsite.Audit.GetEntries(wssQuery);
                                    foreach (SPAuditEntry oAuditEntry in auditCol)
                                    {
                                        if (oAuditEntry.Event.ToString()  == "View")
                                        {
                                            string[] strArray = oAuditEntry.DocLocation.ToString().Split('/');
                                            if (strArray[strArray.Length - 1] == strListItemName)
                                            {
                                                if (viewedUserName == string.Empty)
                                                {
                                                    viewedUserName = oWebsite.AllUsers.GetByID(Convert.ToInt32(oAuditEntry.UserId)).Name;
                                                }
                                                else
                                                {
                                                    viewedUserName += "/" + oWebsite.AllUsers.GetByID(Convert.ToInt32(oAuditEntry.UserId)).Name;
                                                }
                                                strListItemName = string.Empty;
                                            }
                                        }
                                       
                                    }
                                }
                                string[] arrViewedUsers=viewedUserName.Split('/');
                                string[] arrNotViewUsers = arrAllUsers.Except(arrViewedUsers).ToArray();
                                foreach (string arr in arrNotViewUsers)
                                {
                                    DataRow dr = dtTable.NewRow();
                                    dr["DocumentName"]=oListItem.Name;
                                    dr["UnViewedPeopleName"] = arr;
                                    dtTable.Rows.Add(dr);
                                }
                                viewedUserName = string.Empty;
                            }
                        }
                    }
                }
                oWebsite.Dispose();
            }
            DataTable dt = dtTable;
        }

Get SharePoint webservice list to DataTable

       public DataTable GetCountryDataTable()
        {
            DataTable dtTable = null;
            try
            {
                SharePointList.Lists oList = new SharePointList.Lists();
                oList.PreAuthenticate = true;
                oList.Credentials = new System.Net.NetworkCredential(UserName, Password, Domain);
                oList.Url = Url;
                XmlNode itemCollection = oList.GetListItems("CountryList", string.Empty, null, null, "0", null, "");
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(itemCollection.OuterXml);
                DataSet ds = new DataSet();
                StringReader stringReader = new StringReader(xmlDoc.OuterXml);
                ds.ReadXml(stringReader);
                stringReader.Dispose();
                if (ds.Tables[1].Rows.Count > 0)
                {
                    dtTable = ds.Tables[1];
                }
            }
            catch (Exception ex)
            {
                Err.ErrorLog(HttpContext.Current.Server.MapPath("Logs/ErrorLog"), "GetCountryDataTable Method:" + ex.Message);
            }
            finally
            {
            }
            return dtTable;
        }

select all colum from sharepoint webservice list and fetch more than 5000 rows by paging

        public .DataTable GetSchedulerCalendarDataTable()
        {
            System.Data.DataTable dtTable = null;
            try
            {
                SharePointList.Lists listService = new SharePointList.Lists();
                string listName = "Scheduler Calendar";
                listService.Credentials = new NetworkCredential(UserName, Password, Domain);
                XmlNamespaceManager nsmgr;
                XmlNode listView = listService.GetListAndView(listName, null);
                nsmgr = new XmlNamespaceManager(listView.OwnerDocument.NameTable);
                nsmgr.AddNamespace("a", "http://schemas.microsoft.com/sharepoint/soap/");
                List<String> fieldNames = new List<string>();
                foreach (XmlNode field in listView.SelectNodes("a:List/a:Fields/a:Field", nsmgr))
                {
                    XmlAttribute attr = field.Attributes["Name"];
                    // it should not be null, but we check it
                    if (attr != null)
                    {
                        // we store all fields in a collection for later use
                        fieldNames.Add(attr.Value);
                    }
                }
                // get data from list
                XmlDocument xmlDoc = new System.Xml.XmlDocument();
                XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
                XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
                XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
                ndQueryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>";
                ndQuery.InnerXml = "<OrderBy><FieldRef Name='ID' Ascending='TRUE' /></OrderBy>";
                XmlNode positionNext = null;
                while (true)
                {
                    XmlNode items = listService.GetListItems(listName, null, ndQuery, ndViewFields, "1000", ndQueryOptions, null);
                    nsmgr = new XmlNamespaceManager(items.OwnerDocument.NameTable);
                    nsmgr.AddNamespace("z", "#RowsetSchema");
                    nsmgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
                    XmlNodeList itemNodeList = items.SelectNodes("rs:data/z:row", nsmgr);
                    foreach (XmlNode itemNode in itemNodeList)
                    {
                        foreach (String fieldName in fieldNames)
                        {
                            String wsFieldName = "ows_" + fieldName;
                            XmlAttribute attr = itemNode.Attributes[wsFieldName];
                            // if the attribute is missing, we should add it
                            if (attr == null)
                            {
                                attr = itemNode.OwnerDocument.CreateAttribute(wsFieldName);
                                itemNode.Attributes.Append(attr);
                            }
                        }
                    }
                    DataSet listDataSet = new DataSet();
                    // read the result into a data set
                    XmlTextReader readerListDataSet = new XmlTextReader(items.OuterXml, XmlNodeType.Document, null);
                    listDataSet.ReadXml(readerListDataSet);
                    if (listDataSet.Tables[1].Rows.Count > 0)
                    {
                        foreach (DataRow item in listDataSet.Tables[1].Rows)
                        {
                            if (dtTable == null)
                            {
                                dtTable = GetExcelSheetDataTable();
                            }
                            DataRow dr = dtTable.NewRow();
                            string strartDate = string.Empty;
                            if (item["ows_EventDate"] != null) strartDate = (item["ows_EventDate"].ToString() == string.Empty) ? string.Empty : Convert.ToDateTime(item["ows_EventDate"].ToString()).ToString("MM/dd/yyyy");                           
dtTable.Rows.Add(dr);
                        }
                    }

                    positionNext = items.SelectSingleNode("//@ListItemCollectionPositionNext");
                    if (positionNext == null)
                        break; // break if it is the last page
                    else
                        // set next page info if there is more page
                        ndQueryOptions.InnerXml = "<Paging ListItemCollectionPositionNext='" + positionNext.InnerXml + "' />";
                }

            }
            catch (Exception ex)
            {
                Err.ErrorLog(HttpContext.Current.Server.MapPath("Logs/ErrorLog"), "getSchedulerCalendarDataTable Method:" + ex.Message);
            }
            finally
            {
            }
            return dtTable;
        }

101% shares

101% shares
about me