Pages

Showing posts with label Sharepoint 2010. Show all posts
Showing posts with label Sharepoint 2010. Show all posts

Monday, May 19, 2014

Count Related Field

How many times we have cursed SharePoint for not able to get the item count natively without writing code or using workflow. There is an easier way which I stumbled up recently but may be some of you have already figured this out earlier. I wanted to document this so this can be useful for someone like me who is always interested in code.

Recently when I was working on a project where there is absolutely no server side code, pretty much everything was done at the client side. There was a need to get item count per category. This is simple on the list because we could just do a group by and we would know how many items in a particular category, but we wanted to use those number to draw chart. The group by numbers will not be useful and we cannot use char web part (yes I know chart part is gone in 2013 but this project is still in 2010).

How do we do this? We had a lists of categories in a list which is used as lookup for the categories column. We initially thought of writing a client side script that would count items and add it to a field. But this needs to be triggered every time a new item is added to the list. I was trying to create a new field and accidentally I chose lookup field and I noticed that there is a field called count related in the options when i chose the looked up list a source. When I created a field I could immediately see item count for all the items in the lookedup list. Form now on this becomes easy to use this chart part or anything you want even in code that would need to do count.

Figure shows how to create Count Related field

Create new List column Lookup









Item count corresponding to categories



Please leave your feedback in comments.

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());

}


Wednesday, July 11, 2012

Content Type Syndication-Part 2


Content Type Syndication (CTS) is a new feature in SharePoint 2010 which is used for Centralization and Sharing of Content Types between site collection and even Farms using Managed Metadata Service application.  For more information on content type syndication and business case for using it please refer here.
In this blog we will see how to configure content type syndication and how to publish and subscribe content types and metadata.

How CTS works :

The figure gives a picture of how content type syndication works and it general architecture.




 
Managed Metadata service acts as a channel to service these content types to the subscriber sites. This is possible because of the service architecture in SharePoint 2010. As shown in the figure the same publisher can be used to publish to different web application using Shared Managed Metadata Service application.
  • ·         Publisher = Content Type Syndication hub
  • ·         Subscriber = is Consumers of published content type

 

How to Configure



Let us now see how we can configure this in our environment. I created a site collection within my web application called “Content Type Syndication Hub”. Please create a site collection in the web application that you want to serve a publisher for content types.







Once you created the site next step in the process is to activate the content type syndication feature at the site collection level. This is to make the current site collection as the hub or source for our consumer sites.







We now have the site collection that can serve content types to any subscriber who wants to consume these content types. But we haven’t provided the channel to serve these content types. We have to create a managed metadata service as conduit.
Let us now see how we can create Manage Metadata Service.
Service application needs to be created in Central Admin 

Application Management >Service Application > Manage Service Applications









Create a New Managed Metadata Service.








Remember to specify the site collection that we created as the content type hub











Now all is set to use the site collection as content type hub and now we need to create a content type and publish it so that consumer site collections can subscribe to the content types from here.
Let us see how we can publish the content type. We need to create a content type for our test purpose and I created one called “Test” with a site column “test”.








We have all we need for the content type to be syndicated to other site collection. This process is pretty straight simple and we will see how we can achieve this.











While you are in Content Type information page click on Manage Publishing for this Content Type, the publishing content type information page will have option to publish and since I have already published my content type while writing this blog I get to see 2 other option to “Unpublish” and “Republish”. If we click ok on the page will publish the content type to all the site collection that is subscribing from t this site collection.








If you now navigate to any site collection that subscribes from this site collection we will be able to see the Content Type published.








If you look at the content type information page of this content type you will have restricted option compared to the content type that is native to this site collection.







If you do not see the content type published as soon as you click ok check your timer job that helps in pushing these updated to all subscriber sites. SharePoint has 2 timer jobs defined that pushes the content type publishing to other sites in the web application. These usually run every hour and you can force it to run immediately if you want it to be pushed immediately.






Well done we have out content type published. Please share your thoughts in comments.