SharePoint Tools

Published by on
Tags:

This is a list of SharePoint tools/software links. This list will expand as I find more and remember where I found the ones I currently use.

SharePoint Retrieve Task Assigned To User and Groups

Published by on
Tags:

If you want to retrieve a list of tasks that have been assigned to both a user and the groups he belongs to then use the following query:

 

Using this and reflecting the code for the Microsoft.SharePoint.WebPartPages.UserTasksWebPart it is quite easy to make an aggregation webpart to display the list of tasks on a users MySite.

The above query was not thought up by myself but comes from Murilo Rodrigues in a response to Assigning SharePoint tasks to groups.

SharePoint SPSecurity.RunWithElevatedPermissions

Published by on
Tags:

Ever find yourself thinking there must be a quicker/nicer way to run code with elevated permissions instead of the standard:

//run this block as System Account
Guid siteId; //get site id
Guid webId; //get web id
SPSecurity.RunWithElevatedPrivileges(delegate()
  {
    using (SPSite site = new SPSite(siteId))
    {
       using (SPWeb web = site.OpenWeb(webId)){
	 //DO some work
       }
    }
  } 

Instead of writing all this I have written a wrapper class:

    public class SPElevatedPermissions : IDisposable
    {
        public delegate void CodeToRunElevated();

        private SPWeb _elevatedWeb;
        private SPSite _elevatedSite;

        public SPWeb ElevatedWeb { get { return _elevatedWeb; } }
        public SPSite ElevatedSite { get { return _elevatedSite; } }

        public SPElevatedPermissions(SPWeb originalWeb)
        {
            Guid siteId = originalWeb.Site.ID;
            Guid webId = originalWeb.ID;

            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                _elevatedSite = new SPSite(siteId);
                _elevatedWeb = _elevatedSite.OpenWeb(webId);

            });
        }

        public void RunCode(CodeToRunElevated secureCode)
        {
            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                secureCode();
            });
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (_elevatedWeb != null) _elevatedWeb.Dispose();
            if (_elevatedSite != null) _elevatedSite.Dispose();
        }

        #endregion
    }

As you can see from above in the constructor we create the elevated webs and site, we can then use them as below without needing the code to be wrapped in the SPSecurity.RunWithElevatedPermissions method because the majority of code just needs access to a web/site that is the context of the elevated user:


SPWeb web = SPContext.Current.Web;

using (SPElevatedPermissions elev = new SPElevatedPermissions(properties.OpenWeb()))
{  
  SPList  list = elev.ElevatedWeb.List[listId];
  //do other work   
}

However if you still need for some reason to actually run the code itself in elevated mode we can call the RunCode() method:


SPWeb web = SPContext.Current.Web;

using (SPElevatedPermissions elev = new SPElevatedPermissions(properties.OpenWeb()))
{  
  elev.RunCode(()=>{
    SPList  list = elev.ElevatedWeb.List[listId];
    //do other work   
  });
}

We can also run multiple code blocks without worrying about having to instantiate the elevated web and site again or worrying about their scoping:


SPWeb userWeb = SPContext.Current.Web;

using (SPElevatedPermissions elev = new SPElevatedPermissions(userWeb))
{
  elev.RunCode(()=>
  {
	SPList  list = elev.ElevatedWeb.List[listId];
	//do other work 
  }); 
  //do something else here
  SPList userList = userWeb.List[listId];	

  SPList anotherList = elev.ElevatedWeb.List[listId2];
  //process something else	 
}

SharePoint Unsecured Application Page

Published by on
Tags:

According to MSDN the class Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase, Microsoft.SharePoint should be used to give anonymous users access to page. However despite in inheriting from the class you might still get a 401 Unauthorised error. Not only do you need to inherit from this class but you also need to override the AllowAnnoymouseAccess property and set it to return true:

public class Login: Microsoft.SharePoint.WebControls.UnsecuredLayoutsPageBase{
	 protected override bool AllowAnnonymouseAccess{
	 	 get{
	 	 	 return true;
	 	 }
	 }

}

I am not sure why this class needs this additional property but once added it worked fine.

 

All blogs tagged with 'sharepoint'

Projects

Have you read?

Using anonymous types for databound web controls



Blogs by date