I diffidently agree with Dave Paylor. However it's not too complicated to get it done throw some code, all you need is an Event Receiver with the following settings supposing that you already have a library with a column of type "Single line of text" that shall hold the domain name:
- Event type: List Item Events.
- Event Source: The list you are working on, custom or OOTB.
- Event handled: ItemAdded.
Here is the code to have in the CS file of the event receiver:
SPList docLib = properties.Web.Lists.TryGetList("Name of the document library");
SPField companyName = docLib.Fields["Name of the text field that will hold the subcompany name"];
string subCompanyName = System.Environment.UserDomainName;
companyName.DefaultValue = subCompanyName;
Every time a document is added to the library the domain name of the user who added the document will be inserted in the field.
Just in case you're up to add this particular text field programmatically, use below code in a feature receiver inside "event raised after a feature has been activated" block:
SPSite rootSite = (SPSite)properties.Feature.Parent;
SPWeb rootWeb = rootSite.RootWeb;
SPListCollection collList = rootWeb.Lists;
SPList docLib = collList.TryGetList("Name of the document library");
SPFieldCollection collFields = docLib.Fields;
SPField subCompanyName = null;
if (collFields.ContainsField("Name of the text field that will hold the subcompany name") == false)
subCompanyName = new SPField(collFields, "Text", "Name of the text field that will hold the subcompany name");
SPView defaultView = docLib.DefaultView;
defaultView.ViewFields.Add("Name of the text field that will hold the subcompany name");
catch (Exception ex)
throw new Exception(ex.ToString());
As i said, i would recommend Dave Paylors answer upon mine as not everything should be done throug coding when you can do it in an easier way.