3

My environment is sharepoint 2010 and vs 2010.

I already have an OOTB document library that I am using and I wanted to know how I can add a tag or keyword to a METADATA column.

I am using the same document library in all subsites and so would like to tag the uploaded document with the name of the current site (ie if it's being used in a subsite, that subsite's name would be the tag).

Would I need to build a custom document library or some sort of add-on webpart that attaches to the existing document library?

If you could provide some code samples if any that would be appreciated.

thanks,

KS

UPDATED

So I deployed the below EventReceiever, however it does not appear to be doing anything - when I attach the debugger it says the break point could not be hit no symbols loaded - what should I do?

public class EventReceiver1 : SPItemEventReceiver
{
   /// <summary>
   /// An item is being updated.
   /// </summary>
   public override void ItemUpdating(SPItemEventProperties properties)
   {
       SPWeb Site = null;
       using (SPSite SiteCollection = new SPSite(properties.ListItem.ParentList.ParentWeb.Site.ID))
       {
           Site = SiteCollection.OpenWeb(properties.ListItem.ParentList.ParentWeb.ID);
       }

       //if (properties.ListId.Equals("eaff7d9c-9209-47cc-af84-1488faefe793"))
       //{
           string fieldInternalName = properties.List.Fields["TaxKeyword"].InternalName;
           properties.AfterProperties[fieldInternalName] = Site.Name;
       //}
   }
}

MORE UPDATES CurrentSite is always nothing (the root) whether I use the document library webpart in a subsite or not.

public override void ItemAdded(SPItemEventProperties properties)
{
  string CurrentSite = properties.Web.Url;
  base.ItemAdded(properties);
}

CONCLUSION It is not possible to retrieve the subsite name from an event receiver fired from a document library which "lives" in the root, but displayed on a webpart in a subsite.

I will have to try something else!

thanks to all.

1
  • "break point could not be hit no symbols loaded" : most of time, this means either the debug mode is not enabled (compilation mode in VS or debug="true" in the web.config file), or that the deployed version is not the same as your current code.
    – Steve B
    May 2, 2012 at 12:13

5 Answers 5

2

I'm not sure exactly what you mean by you are using the same document library for all subsites. But I think you do this ootb with the Column default value settings under your list settings.

update:

    public override void ItemUpdating(SPItemEventProperties properties)
    {
        properties.AfterProperties["SiteUrl"] = SPContext.Current.Web.Url;
        base.ItemUpdating(properties);
    }

I haven't tested the above code but that should get you started. YOu will also include the same code for itemadding

9
  • I used sharepoint designer to export the document library from the root site to the Site Gallery and then used this webpart on pages on my subsites. Therefore allowing users to store documents centrally.
    – Perplexed
    Apr 20, 2012 at 0:30
  • Ok, I think I understand. I think you will need to accomplish this with an event receiver. I'll update my answer above with some code. Apr 20, 2012 at 2:25
  • Thanks for your response - how does this receiver communicate with the document library upload process?
    – Perplexed
    Apr 20, 2012 at 3:20
  • Maybe you should check out the documentation: msdn.microsoft.com/en-us/library/gg749858.aspx Apr 20, 2012 at 11:19
  • Ok, I am looking into the event reciever example (altho the microsoft example applies the setting to all document libraries). thanks.
    – Perplexed
    Apr 20, 2012 at 13:32
2

I'd set the columns default value using MetadataDefaults.SetFieldDefault. Instead of doing it in PowerShell you can do it in a FeatureReceiver of the feature deploying your document library

1

You can for each document library also specify different default values for columns. You can even specify different default values for different folders within a document library. Navigate to the settings page of the document library and click the link labeled Column default value settings

See the article Metadata Defaults in SharePoint Server 2010 for more details.

1
  • This would work - but I need for the default value to be the site that is currently hosting the document library webpart. The doc lib from the root site was exported as a webpart and put into various pages on different subsites. When a document is added from any of these subsites I need the document to be tagged with the name of the subsite. Is there some code we can put in the default value field or is it just static text?
    – Perplexed
    Apr 20, 2012 at 13:31
1

As per my question edit - it is not possible to retrieve the subsite name.

What I did instead was to create a subsite document library and then when adding a document to it, I move it to the root document library along with the metadata. I have not finished doing this as yet, but I'm almost there.

For the subsite page, I still display the root document library, but I inserted a hyperlink manually which points to the local subsite library (if that makes sense).

thanks.

0

You can use the article Setting a Default Value for a Managed Metadata Column for more details for how this can be achieved on Miriam's blog

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.