Monday, June 3, 2013

Get a count of ALL Lists and Document Libraries in your SharePoint Site



I needed to provide a report of all List and Document Libraries from my SharePoint Site/Child Sites and SharePoint Out of the Box does not provide this feature.
There are however, various options to getting this report, some which are listed below. The various options may give you site count from the top-level sites, but not child sites.
Options:
1.  Use SharePoint Designer reports:– Only gives top-level result
2.  Use SPSite  and SPWebCollection:– This involves writing code and iterating though your collection.
3.       Use Client Object Model as shown here on MSDN:- http://msdn.microsoft.com/en-us/library/ee538683%28v=office.14%29.aspx. Here again you have to iterate and get your data.
4.       My favorite is using good old SQL. This assumes that you have access to SQL Server management Studio and that you have the necessary credentials to your SharePoint SQL. Note that accessing your SQL directly is not recommended by Microsoft, but sometimes you have to do what you have to doJ So here is the SQL that will give you ALL List and Documents throughout your Site:

SELECT     AllLists.tp_Title, Webs.SiteId, Webs.FullUrl, AllLists.tp_RootFolder, Webs.ParentWebId
FROM         AllLists INNER JOIN
                      Webs ON AllLists.tp_WebId = Webs.Id
ORDER BY Webs.FullUrl

Feel free to refine it to your needs and share your successes.
I hope this helps someone out there.

Friday, May 3, 2013

SPWeb.EnsureUser Method throws Access Denied Error

According to Microsoft, SPWeb.EnsureUser  Checks whether the specified login name belongs to a valid user of the Web site, and if the login name does not already exist, adds it to the Web site.

Well, this will not work in an environment where you have membershipprovider enabled. to get around this issue please use the following code. 


#region Add user to a group
        /// <summary>
        /// addUserToGroup
        /// </summary>
        /// <param name="groupName"></param>
        /// <param name="userLoginName"></param>
        public void addUserToGroup(string groupName, string userLoginName)
        {
            SPSite site = new SPSite(RootWebUrl);
            SPWeb web = site.OpenWeb();
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                          {
                              web.AllowUnsafeUpdates = true;
                              SPUser spUser = web.AllUsers[userLoginName];

                              if (spUser != null)
                              {
                                  SPGroup spGroup = web.Groups[groupName];
                                  if (spGroup != null)
                                      spGroup.AddUser(spUser);
                              }
                          });
            }
            catch (Exception ex)
            {
                ErrorLogger.LogFeatureError("Error Adding user to SharePoint Group: " + groupName + " Name: " + userLoginName, ex);
            }
            finally
            {
                web.AllowUnsafeUpdates = false;
                site.Close();
                web.Close();
            }
        }
        #endregion



 #region Create user
        /// <summary>
        /// CreateUser - Create a SharePoint User and assign role
        /// </summary>
        /// <param name="strLoginName"></param>
        /// <param name="strEMail"></param>
        /// <param name="strName"></param>
        /// <param name="strNotes"></param>
        /// <param name="strSiteURL"></param>
        /// <returns></returns>
        private SPUser CreateUser(string strLoginName, string strEMail,
                    string strName, string strNotes, string strSiteURL)
        {

            SPUser spReturn = null;
            SPSite spSite = null;
            SPWeb spWeb = null;

            try
            {
                //Open the SharePoint site
                spSite = new SPSite(RootWebUrl);
                spWeb = spSite.OpenWeb();

                SPSecurity.RunWithElevatedPrivileges(delegate()
                       {

                           spWeb.AllowUnsafeUpdates = true;
                           spWeb.SiteUsers.Add(strLoginName, strEMail, strName, strNotes);
                           //Update site
                           spWeb.Update();
                           spReturn = spWeb.AllUsers[strLoginName];
                       });
                //Response.Write("User Successfully Created= " + strLoginName + "<br>");
                ErrorLogger.LogFeatureMessage("User Created", "User Successfully Created= " + strLoginName);
            }
            catch (Exception ex)
            {
                //Response.Write(ex.Message.ToString());
                ErrorLogger.LogFeatureError("Error Creating SharePoint User", ex);
            }
            finally
            {
                spWeb.AllowUnsafeUpdates = false;
                spWeb.Close();
                spSite.Close();
            }

            return spReturn;
        }
        #endregion

Server Error in '/adfs' Application


Ok , all of a sudden our SharePoint refused to render pages, I mean drop dead with this error.


We checked all the necessary culprits but no resolution. Our environment had just recently been Virtualized, which introduced a lot of unknowns into the equation.

After much digging around, come to fine out the time on our server were out of synch, go figure.
So, I hope this saves someone precious time out there.......:)