0

I want to create a column created/added when I create a document library or splist within my site collection. Will the approach of :

  • ListCreated event receiver work in this situation?
  • Should I creat a timer job which checks for list creation in every 1 minute interval in my site collection?
  • Am using SP 2013 on-premises with VS 2013 IDE
2

Out of the two options, event receiver is the best approach. Deploy your event receiver with a web-scoped feature and handle ListAdded event:

Event Receiver:

<?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Receivers >
          <Receiver>
            <Name>EventReceiverListAdded</Name>
            <Type>ListAdded</Type>
            <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
            <Class>Class.EventReceiverListAdded</Class>
            <SequenceNumber>10000</SequenceNumber>
          </Receiver>
      </Receivers>
    </Elements>

List Added:

public override void ListAdded(SPListEventProperties properties)
{
    base.ListAdded(properties);

    SPWeb propertiesWeb = properties.Web as SPWeb;
    SPSite propertiesSite = propertiesWeb.Site;

    using (SPSite site = new SPSite(propertiesSite.ID))
    {
        using (SPWeb web = site.OpenWeb(propertiesWeb.ID))
        {
            var list = propertiesWeb.Lists[properties.List.ID];
            list.Fields.Add("Your Column", SPFieldType.Text, true)
            list.Update();
        }
    }
}

Just a note: I always tend to open a new web or site objects and get lists/items as they might be changed by other event receivers. This way the solution is more scalable if you don't use properties.List/Web/Site/ListItem.

  • appreciate your timely help,Paul. – userAZLogicApps Dec 9 '15 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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