Sharepoint Tips And Tricks is Ishai Sagi's sharepoint information blog. It specializes in Microsoft SharePoint technologies, including web parts, development, configuration, customization, and best practices for the use of Microsoft SharePoint Server and Windows SharePoint Services. It also provides some related Office Information, including VSTO and VSTA and other office application development tips.
Thursday, May 17, 2012
SharePoint Upgrade or Migrate
In March I have published an article in IDM magazine (australia) about upgrading sharepoint and what is the difference between that and migration, and the pros and cons of each one. I even gave tips to successfull upgrade\migration projects. If you want to read it, I have now republished it in my company's site:
http://www.extelligentdesign.com/Articles/UpgradingSharePoint.aspx
Feel free to drop me comments on the article here (and please feel free to tell people about it).
Monday, May 14, 2012
Useful Event handlers for SharePoint
The product I am now selling as part of my Company's offerings is called "Extelligent Design's SharePoint List Events Package". The idea is that I will keep adding event handlers to that package, so you pay once, and get a whole lot of functionality. The event handlers are as flexible as possible - allowing you to configure what permissions are assigned and to whome, or what format the title of the list items or documents should be, and even hide the title field from users in the editing forms.
If you purchase, not only do you get the code compiled, you also benefit from 1 year support (patches and added features).
To read more, and to purchase, go to the product's page in our web site.
Monday, February 06, 2012
Enabling Managed Metadata in Office365 (includes download)
There are two possible reasons. The first - you don't have a hosted site that supports managed metadata. To check if that is the case, check with your hosting company, or, if you have Office365, you should have a "manage term store" link in your sharepoint online administration centre. If you dont have that link, then you cannot have managed metadata - sorry.
The other possibility is because a feature that is needed on the site is not enabled by default, and because it is a hidden feature you will not be able to activate it yourself.
If you search for this, you will find a lot of articles about how to solve it on a regular sharepoint farm using either command line or powershell. However, what happens if you don't have access to the server, such as in the case of SharePoint online (Office365) or a hosted environment?
In such a case, you need to use a sandboxed solution to enable the feature. To simplify this, I have created a sandbox solution that has a single feature called "Enable Managed Metadata":
This feature simply activates and deactivates the managed metadata feature.
To download this solution, visit my company's web site's code sample library or click this direct download link.
After you have downloaded the solution, you need to upload it to your site. Go to the site's settings page, and click the "Solutions" link to go to the solution gallery. Here, click on "Solutions" in the ribbon and then "upload solution" button. Browse to the file you downloaded, and upload the file. Once uploaded you will have the option to Activate the solution. This should also automatically activate the feature, so from that moment on you can start creating managed metadata columns!
While you'r there, would you mind clicking the "like" button for our web site?
Sunday, January 15, 2012
The importance of synchronous event handlers
In SharePoint, we can create list item event handlers (or receivers). I have written about this in the past, and even posted some code for setting the security automatically on a list item as it is created.
Now, there is one thing I wanted to write about this topic. Event handlers can be defined as either Synchronous or Asynchronous - regardless of the event type. By default, events were always asynchronous if they had the "ed" suffix (ItemAdded, ItemUpdated), and synchronous if they had the "ing" suffix (ItemAdding, ItemUpdating). However, sometimes we want to run our code in an "ed" event, but have it synchronous (notice that the opposite is impossible have the "ing" properties to run asynchronously)- for example, when we want to modify item properties when its added, but before the user has been redirected to a new page.
A classic example is the upload of a document to a document library. The user gets prompted to browse for a file and clicks upload. The next page would be the properties page for the file that was uploaded. If we want to set some of the properties to be pre-filled for the user, we have to do that after the file has been uploaded - but we cannot do that in ItemAdding, as at that point we don't have access to the item - it wasn't created yet. In ItemAdded we can set values to the item - but by default this event is asynchronous - and sharepoint will show the user the properties dialog while the event is still happening in the background. The result is disastrous : the user sees the properties screen, clicks "Save" and gets an error telling him or her that the item has been modified by another process or user.
To resolve this we use the "Synchronization" XML tag in the receiver's registration XML with the value set to "Synchronous" or, if the receiver is registered using server object model code, we set the "Synchronization" property of the receiver class to SPEventReceiverSynchronization.Synchronous. To quote MSDN: "Synchronous processing provides the ability to run the event in the same thread before sending the Web response back to the browser."
As an example, lets create an event receiver that sets the title of a file to the file name, as the file is being uploaded. The code is simple:
public override void ItemAdded(SPItemEventProperties properties) { base.ItemAdded(properties); if (properties.ListItem["Title"] == null) { int startingPoint = properties.AfterUrl.LastIndexOf("/"); properties.ListItem["Title"] = properties.AfterUrl.Substring(startingPoint + 1); properties.ListItem.Update(); } }
Now, the XML that registers the receiver is :
<Receivers ListTemplateId="101"> <Receiver> <Name>SetTitleFromFileNameItemAdding</Name> <Type>ItemAdded</Type> <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly> <Class>DocumentTitleSetter.SetTitleFromFileName.SetTitleFromFileName</Class> <SequenceNumber>10000</SequenceNumber> <Synchronization>Synchronous</Synchronization> </Receiver> </Receivers>
And when they upload, they see the dialog allowing them to change the file's name, and by magic the title is already filled for them!
Where can this be useful? I can think of a very real world example: assigning a number to a document as it is being uploaded. The title setting event can be critical in some scenarios where we have lists with lookup columns to a document library - since the lookup column cannot show the file name column we need to show the title, which means we cannot have an empty title (in which scenario you will also want to set the title to unique, and add extra code to make sure the title you are setting is not unique - dont assume that because it is a copy of the file name there wont be duplicates - remember that users may edit both file names and titles!).
Just in case someone finds this useful, I am uploading the code, and the compiled solution package to my company's web site as a free, open source solution for anyone to use on their own risk. I have tested this to work with Office365 (SharePoint online) and as a farm solution - so enjoy!. You can find all my company's free code samples and solutions in our document library on our web site. The specific files are: [Source Code] and [Sandbox Ready Package].
Wednesday, November 16, 2011
Event handler to archive items when deleted
A person just asked this on a mailing list I am on, so I thought I'd share this code. The requirement - copy a list item that is deleted to an archive list. The solution - event receiver that copies the item. The code below does the job, but again is only a sample - you still need to implement error handling, and there are hard coded variables there you will want to change.
Note: this code is specifically for list items, not for documents in document libraries. You can easily change the code to support documents as well (see the CopyAttachments function for an example on how to copy files).
public override void ItemDeleting(SPItemEventProperties properties) { //note: may require permission elevation. //TODO: add error handling //get the item being deleted SPListItem item = properties.ListItem; //get the target list SPList targetList = properties.Web.Lists["Announcements Archive"]; //create the new item SPListItem newItem = targetList.Items.Add(); //copy the list item to the target foreach (SPField f in item.Fields) { if (!f.ReadOnlyField && newItem.Fields.ContainsField(f.InternalName)) newItem[newItem.Fields.GetFieldByInternalName(f.InternalName).Id] = item[f.Id]; } //copy "special" read only fields that can be written to newItem["Created By"] = item["Created By"]; newItem["Modified By"] = item["Modified By"]; newItem["Modified"] = item["Modified"]; newItem["Created"] = item["Created"]; newItem.SystemUpdate(false); CopyAttachments(item, newItem); base.ItemDeleting(properties); }
Note - for the "CopyAttachments" I used code that I published in the past: http://www.sharepoint-tips.com/2008/11/how-to-copy-attachments-from-one-list.html
Tuesday, November 15, 2011
Setting item permissions for new items
A common question that I recently answered in a sharepoint conference is how to make a sharepoint list only show items to the users who wrote the items, and users specified in the item's properties.
A good example of this is a task list where only the original creator of the task and the assignee can see and change the task. This is impossible to do in sharepoint out of the box, and that is where we, as developers, come in.
The way to do it is by developing a simple event handler and attaching it to the list. I prefer to attach to ItemAdded and ItemUpdated - put the exact same code in both events (so that people can reassign the task). The code sample below is a good starting point for you to figure out how to do this. The code sample assumes you know how to use visual studio to create a new event handler, and is meant only as a sample - it doesn't have error handling and it does have some hard coded values for a task list that you may want to change.
public override void ItemAdded(SPItemEventProperties properties) { //get the list item that was created SPListItem item = properties.ListItem; //get the author user who created the task SPFieldUserValue valAuthor = new SPFieldUserValue(properties.Web, item["Created By"].ToString()); //get the "assigned to" user. Note - this will throw an error if the task is not assigned to anyone - implement error handling! SPFieldUserValue valAssignedTo = new SPFieldUserValue(properties.Web, item["Assigned To"].ToString()); //disconnect the security from the list, and delete all permissions item.BreakRoleInheritance(false); //create the object that will hold the roles for the author user SPRoleAssignment authorRole = new SPRoleAssignment(valAuthor.User); //create the object that will hold the roles for the assigned to user SPRoleAssignment assignedToRoles = new SPRoleAssignment(valAssignedTo.User); //get the contribute role from the web. Alternatively use code to create a new role definition with custom permissions. SPWeb oWebsite = properties.Web; SPRoleDefinitionCollection collRoles = oWebsite.RoleDefinitions; SPRoleDefinition oRoleDefinition = collRoles["Contribute"]; //assign permissions to task author authorRole.RoleDefinitionBindings.Add(oRoleDefinition); item.RoleAssignments.Add(authorRole); //assign permissions to task assignee assignedToRoles.RoleDefinitionBindings.Add(oRoleDefinition); item.RoleAssignments.Add(assignedToRoles); //update the item item.Update(); base.ItemAdded(properties); }
You can download the entire code sample as a visual studio project from my company's site's code samples document library at http://www.extelligentdesign.com/Code%20Sample%20Downloads/Forms/AllItems.aspx. You will want to change the project's properties to point to your local development site before debugging.
Saturday, October 15, 2011
Speaking in the upcoming Asia conferences
Next month I will be in both Singapore and Hong Kong for the Asian sharepoint conferences. If you are coming, why not jump in my session to learn about sharepoint development? Who knows, there may be prizes...
http://www.sharepointconference.asia
Friday, October 14, 2011
Setting the images for a publishing page from a module
Here is a small tip for developers (about time I wrote one again). What do you do if you want to provision a publishing page to a sharepoint site, and set the page's image (or rollup image) during the provisioning?
Well, step one should be obvious - make sure you upload the image you want to display. This can be done in the same feature or a seperate one. I will assume that you already know how do do that (if not, then the MSDN article "how to provision a file" is your friend). The same goes for actually how to build the feature and module that uploads your publishing page (again, refer to the MSDN article, or better yet, see my friend Waldek's article discussing just this).
So what am I talking about? I want to focus on how to set two specific properties on the page - the page's image and the page's rollup image. These two are not regular properties, since they are publishing properties. They do not expect the same kind of value that regular image properties expect (which is the same as a hyperlink - first part is the link to the image, than a space and a comma and then the description of the image). With publishing fields you need to supply the actual HTML that will be shown as the image.
Since this is done in the module, you will have to create a "Property" element in your "File" element in the module. The value of that property should be the HTML, but encoded so that the XML doesnt become invalid. The value (when not encoded) should look like this:
<img alt='This is a picture of a chicken' src='/PublishingImages/chicken.jpg' style='border:0px solid;'>
And the entire property (with the value encoded) should look like this:
<Property Name="PublishingRollupImage" Value="<img alt='AFL peace Team at Whitten Oval' src='/newsandblog/PublishingImages/22-gal-tn.jpg' style='border:0px solid;'>" />
Hope that helps someone!
Saturday, August 13, 2011
Office 365 development
I am a bit excited about office 365.
My company (Extelligent Design) is now a 365 partner, and we aim to support clients implementing it. We want to help people set it up, understand how to use it (my book may come in handy, seeing as the end user interface to the sharepoint online is the same as regular sharepoint) and customise it.
Customising is where the real challange begins. As you probably know, to develop for office 365, you can only use the sandbox - which limits what you can achieve in your customisations. I find that not being able to install application pages (pages that go to the _layouts folder) especially frustrating, and, like a fish out of the water for the first time in its life, I suddenly realise how much I tend to rely on having that resource.
Ok, maybe the fish analogy is a bit much. I can still write web parts, and features with event receivers - not to mention list and library event receivers as well! But this is when I hit another wall - a lot of code I wrote in the past relied on me being able to run sections of the code with elevated privilages. This is gone if you develop a sandbox solution, and is very frustrating. Some obvious things that people will want to do in their hosted sharepoint site rely on code being able to run as other users. Workflows are a great example - having a user with permissions on list A enter data into that list, and then the workflow enters data into another list to which the user does not have permissions. Oh, wait, workflows are also not allowed in hosted sharepoint, so replace the word "workflow" with "event receiver" in the sentance above and you still get the same functionality, and you still hit a wall with not being able to write code that implements that requirement.
PS - the security limitation is blocked for both usual methods of running in elevated privilages mode - you cannot use the security namespace (preveting you from using SPSecurity.RunWithElevatedPrivilages) and you cannot use the SPSite constructore - preventing you from loading a new SPSite object with the system user's token.
So where does this leave us? Not all is doom and gloom - we can still write web parts and event handlers and features - as long as they are not too complex. This is great for deploying look and feel, features that implement site templates and more. I see a lot of developers now focusing on creating turn-key solutions that implement industry-specific templates for sites. Small businesses have a lot in common, and they are the real target of office 365 - so anyone who comes up with a feature that creates lists and libraries that solve a common problem for a small business may hit a jackpot similar to the mobile phone app developers.
Finally, hosting companies that host office 365 may let you deploy code to the servers directly, allowing non-sandbox development to take place. I think this will be hardest to do, since those companies will be very afraid to change their environment from the microsoft default, but a strong partnership with such a company may be a great thing for a sharepoint developer. Come to think of it- if you ARE a hosting company that office 365 hosting and you want some value add to your clients - talk to me!
Office 2010 SP1
Ok, this may not be a sharepoint post, but...
If you are like me, you have "Virtual Clone Drive" installed, allowing you to mount ISO files as virtual CDs (other virutal CD software is available).
If you are like me, you do not have a physical CD drive in your laptop (having pulled it out and put a second SSD in instead).
If you are like me, you are running office 2010.
If you are like me, you always install whatever Windows Update tells you to install.
So...if you are like me, then you have been unable to install service pack 1 of Office 2010, failing (of all things) on installing a sharepoint client component.
The solution turns out to be simple - disable the virtual CD drive and then do the update. Some recommendations from Microsoft also suggests to put a DVD or CD in your physical CD drive while patching. Seems to be a bug in SP1.
Lucky for us sharepoint people that the SharePoint SP1 doesnt have all these annoying installation bugs. Oh, wait...



