Pages

Sunday, March 30, 2014

Related Items Column SharePoint 2013

SharePoint 2013 is out for sometime and there are many new features including Client Side Object Model (CSOM) and new services like Translation Service and APP management etc. I am going to share an interesting thing that I stumbled upon today while setting up some POC using SharePoint for Project Management. This is interesting and useful because it becomes handy for setting up business processes in SharePoint.

Today while setting up the site I happened to create a Task List and was going through all the fields. I noted a field type called "Related Items". We all would have seen "predecessor" field in SP2010 tasks list and "Related Issues" in SharePoint 2010 issues List. I now got curious to find out about this field because it looks new.



First, I clicked on the field in the List settings page to see information about the field. I did not field any information at all in the description. Next step is to check at the site columns. The field is not visible in site columns. This only means as we all know it is hidden. Now curiosity just shot up and really want to know why this field is hidden and what does this field has to offer and how is this field different from"Related Issues" or "Predecessor". One this is surely certain that this field is custom field type unlike other two fields which are lookup fields.





Now setting out to find out what this field has to offer what better way to find than by creating a task. So I created a new task but strangely enough I could not see the field in new form. This now becomes intriguing because if I am not seeing in New Form I am sure if I will see in Edit Form while editing the item or View Form. But I was in the mission of finding this out. So I decided to edit the item because as we all know we can have the property on the field "Show in New Form" to false but still show in edit form.





I was in the edit form and could not see the field. I was getting anxious because how I can fill the field if it not showing up in New and Edit Form.What is the use of the field that is hidden through out and what is SharePoint trying to achieve with this field? These were some of the question I had and decided to check the View Form.We all know that View form is readonly view of Edit Form, so I wasn't excepting anything but thought that it could be information only field that SharePoint auto populates.








With this thought I opened the View form and contrary to my impression i saw a link right across "related items" field. This is the first place I was able to see Related Items tied to an task item in the list. I went ahead and clicked the Link which brought up a Pop up which by default points to the site collection. Eureka. The "related Items" is allowing me to add items that are related to this tasks. I selected a document from document Library and clicked "OK" boom! the document is linked to the tasks. I was able to add multiple items at a time and even was able to upload a document to a library right there in the pop up. So if the document or the item is not available in SharePoint you will be able to add it and then link it all in one shot without having to close the pop up.This is huge.














 Now why do I think this field is useful?

The field is a big step forward where in enterprise when some one is assigned to work on a tasks the person creating the tasks can add all these related items as reference for the other person to work on. For example the project manager can add all reference to all project items corresponding to a tasks as related item.The reference items can be independently managed and updated. Example Project Plan, Resource Availability.We can add reference to any thing that is in SharePoint. It could be an Issue, list item etc.

This to me is better than having as attachment because the references are independent of the tasks item. This is makes a true relational setup and easy to maintain single version of truth that is referenced.

Right now we will only be able to add items in the present site collection but if  future if Microsoft allows us to add items stored externally or in different site collection which would makes this feature a jaw dropping feature in SharePoint until we unearth another hidden feature.

Hope you find this one useful and let me know your thoughts and suggestion in comments. I will be exploring SharePoint 2013 and until then happy SharePointing.

Friday, February 21, 2014

Working with SharePoint List Attachments

SharePoint Lists are a versatile artifact in SharePoint. It can be mend in any way possible to achieve the desired outcome. This is the reason to use List for all the help desk applications. Recently I had to build a helpdesk application but the client wanted to use plain vanilla SharePoint and wanted to avoid custom code and wanted to keep it to bare minimum. Building a simple helpdesk with List and no code is relatively straight forward but the catch here is they already had created a basic structure where they are using a list to collect the information from external users and the information is getting copied to a new list for internal users. This is done to implement separation of permission as mentioned earlier there is no code approach. This is in their POC environment.

Now they wanted to move to production.Implementation is done as follows

1. They are using attachments in the list to get relevant reference documents form external users.
2. Workflow is been used for copying item from one list to other.

The problem that they are facing is attachments are not getting copied. This is interesting because attachments in SharePoint lists are a linked item and is not part of the SharePoint fields. This is what is causing the workflow not to copy the attachment. Also there is no workflow activity that can copy attachment.

The way to solve this problem would be to

1. Create a custom Copy List Item activity with extended functionality to look for attachment and copy it.

2. Customize the List form with Infopath forms and make the attachment to be part of the list item (This I saw working but still could not comprehend why it works.)

3. Write a small event handler (Item Added and Updated(if you are allowing delayed submission) and copy the attachment.

We went with third option and reason for that being though we had to write code we could do it on a Sandboxed solution rather than a Farm solution which would be the case for option 1. Option 2 is ruled out because the client did not like infopath forms. Its good because we now know Infopath form is dead you can read this here)

There is another thing the remember with List Item  attachments. Since List Item Attachments are  stored as collection when you access them in code and these are collection of strings rather than a file. so we have to user getfile method to get the file and add it. Similarly adding an attachment do not need item update as it linked item.

Client is on SP 2010 and I had not  an opportunity to try this out in Client Side Object Model (CSOM or JSOM) which I am planning so that it becomes easy for SP2013 but will update this post after trying with SCOM/JSOM code.

Code :

foreach (string fileName in requestItem.Attachments)
{
  SPFile file =requests.ParentWeb.GetFile(requestItem.Attachments.UrlPrefix + fileName);
                                   properties.ListItem.Attachments.AddNow(file.Name, file.OpenBinary());

}


Monday, February 10, 2014

Set Field Created through UI to readonly

SharePoint fields are excellent form of reusable asset. There are different types of fields that can be created in the SharePoint environment. As a developer we tend to create some fields as readonly which we could achieve easily while creating the solution using FieldXML in the element.xml file. But there are time when we had to create fields from the SharePoint UI which do not provide the flexibility of setting the field readonly. We would come across these scenarios when we want to have a field that will be used in Workflow or even some times we do not what to show the fields in Edit and Newform of the list or library. It would be an overkill if we create a solution for deploying a readonly field.

There is always poweshell who is our best friend who will never let us down in need. A small script would help achieve this. Below is the script that I wrote to set a list field to readonly.



Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the Web
$web = Get-SPWeb "http://sp2010"
#Get the List
$List = $Web.Lists["Daily Email"]
#Get the Field
$Field = $List.Fields["Email User"]
#Set the field to Read only
$Field.ReadOnlyField = $true
$Field.Update()

Note that I am using Display name of the field instead of internal name. Please feel free to extend this  script if you like. Also make sure you are running this script as Admin user.

I will share the next thing I experiment and until then Happy SharePointing.As always if you have any suggestion or question feel free to ask in the comment section so we all can learn together.