Wednesday, June 25, 2014

SharePoint 2010: How to use RunWithElevatedPrivileges

I ran into a bit of a problem, when I needed to change the permissions on an item. Doesn't sound that bad, right? Well, it is, when you only work with an event receiver, which is started by any user. The solution sounded simple, but still took me a while: RunWithElevatedPrivileges.
There are a couple of issues you might come in contact with, so I will try to post a couple of those in this post.

Tips & possible issues:
1. Create everything from scratch!
In one of my first attempts I just used the existing SPWeb I already used. But nope. This will only work, if the user already got enough permissions for his / her actions, which of course means: It did not work. You will have to get your web, lists, items, everthing again if you want to manipulate them with elevated priviledges.

2. Dispose your web!
Yeah, I know: It should always be the case. But believe me, while coding I forgot it twice in my RWEP-code block. The problem with this is pretty simple. The web you were using still exists so it would still be possible to work with this web. Including elevated priviledges. Not that nice, so please just dispose your web.

3. Don't use it all the time!
Yes, you could do that, BUT! you shouldn't. As always use least priviledges need, and for most of the things my event receivers are doing, I don't need a lot of permissions. And so shouldn't you, because otherwise your software concept might have a few problems.

How does it work?
The shortest explanation is this one:
A delegate method that is to run with elevated rights. This method runs under the Application Pool identity, which has site collection administrator privileges on all site collections hosted by that application pool.

And here is the code that made it work:
        using (SPSite site = new SPSite(properties.Web.Url))
              using (SPWeb myWeb = site.OpenWeb())
                     // Add your code!


I only tried this on SharePoint 2010, so maybe it's a bit different on SharePoint 2013 or Office 365.

Monday, June 23, 2014

Office 365: Napa & this App-Thing

To be honest: Everything is an app nowadays, which makes sense. App derived from application, and someone, my guess is Apple, decided to call it "app", because it's shorter and doesn't sound that complex. A concept and a name for the new generation of smartphone, tablet and PC users. And now Microsoft jumped on the band wagon. Well, it was a while ago, but I finally had enough time to check Napa out.

Yesterday I tweeted that I'm going to start to look into apps. Problem is: I'm not a dev, just interested in stuff like this. So this is will be just a first hands-on experience, nothing in-depth, nothing special and of course nothing so solve any problems. Just hands-on. First hands-on.

I simply worked along a tutorial from Microsoft. You will find a link to it in your Office 365 Dev System. Don't get me wrong, but sometimes I have a hard time to start a project. In this case I didn't even know what was going on, because I wasn't sure what Napa actually can do.

For starters: JavaScript & jQuery. And Lots of it. But let's not get ahead of our self. The hole UI is really neat and isn't confusing at all. Everyone who can remember their first time in Visual Studio will know how confusing that was. But take a look at this little snapshot:
Napa in Office 365.
That's it. Nothing more to it. Of course you don't have a lot of features, but my guess is, that you won't need that.
And here's where I'm making a little jump: Just use the tutorial, it's pretty good. I just want to talk about the other stuff.

So, you have your code ready, like I do and you hit the "play" button. A little popup will show up to tell you in which deployment step you are currently at.
Deployment steps in Napa.
While it was deploying my app, I wondered how this thing will start and what it will look like. But luckily Napa is designed for people like me. Click on the link within the popup and your app will show up.
Click on the link.
The first app.
That was easy and fast and free. Yes, it is free to develop with Napa, which is cool. And because it's browser-based (in my case it's Chrome), you can just open your app everywhere and continue coding without needing a development system. But! You need an Office 365 Dev account, which costs something like $99/Year.

While coding, Napa can provide some help, which is also pretty cool and looks create. I like that style.

So after coding my first small app, the biggest question for myself is: Will I ever use this?
Well... I think I will, but I don't know if I will use it like this. Usually when I code I need stuff like permission management, but I don't know if this is possible with Napa and Office 365 Apps.

But I will definitely continue to test Napa and try to port some of my old projects to Office 365, just to see if it would work.