Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Trying to change the title field of a document library to be required..doesn't want to work.

SPList docLib = web.Lists["Process Aids"];
SPField field = docLib.Fields["Title"];
field.Title = "Display Name";
field.Required = true;
field.Update();

Title shows up as Display Name. I saw something that said to allow unsafe updates. I tried that, but got an error saying that security validation was invalid.

share|improve this question
1  
you have then tried web.AllowUnsafeUpdates = true. Where exactly are you using this code (webpart, event receiver....)? Can you include all code you are using: SPWeb web definition for start. –  Vedran Rasol Sep 8 '11 at 14:15
    
It is in the feature activation event receiver. SPSite curSite = (SPSite)properties.Feature.Parent; SPWeb web = curSite.RootWeb; –  WCHolland Sep 8 '11 at 14:45
    
Welcome to SharePoint Stack Exchange! A well written question encourages people to answer it! Please read our guidelines which I've used to improve your post. You can also read How to Ask for more general guidelines. –  Alex Angas Sep 8 '11 at 20:05
add comment

2 Answers

I have tried this code snippet (from console application):

    using(SPSite site = new SPSite("http://mycoolsite"))
    {
        using(SPWeb web = site.OpenWeb())
        {
            SPList list = web.Lists["My List"];
            SPField fldTitle = list.Fields["Title"];
            fldTitle.Required = false;
            fldTitle.Title = "New title";
            fldTitle.Update();
        }
    }

and it works.

Edited: After reading comments I have changed code to FeatureActivated event:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWeb web;
        SPSite site;

        //Parent can be site or web depending on feature scope
        object oParent = properties.Feature.Parent;
        if (properties.Feature.Parent is SPWeb)
        {
            web = (SPWeb)oParent;
            site = web.Site;
        }
        else
        {
            site = (SPSite)oParent;
            web = site.RootWeb;
        }

        SPList list = web.Lists["My List"];
        SPField fldTitle = list.Fields.GetFieldByInternalName("Title");
        fldTitle.Required = false;
        fldTitle.Title = "New title";
        fldTitle.Update();

    }

If you have some 'strange' permissions on your web and if user have permissions to activate feature and doesn't have permissions on list you could wrap code in SPSecurity.RunWithElevatedPrivileges as @VadimLozinskiy suggested.

To summarize: I have tested above code and I really think is valid. If you are still receiving errors on feature activation your problem is elsewhere. To troubleshoot it try to build another simple feature using above code to test if your environment is 'acting as expected'.

share|improve this answer
    
Unfortunately, that did not get the job done for me. I don't think it makes a difference, but I'm trying to do this in a feature receiver for a publishing page. –  WCHolland Sep 8 '11 at 14:44
add comment

Hi WCHolland try this code

SPSecurity.RunWithElevatedPrivileges(delegate
{
using(SPSite curSite = new SPSite(((SPSite)properties.Feature.Parent).Url)
    {
        using(SPWeb web = curSite.OpenWeb())
        {
          SPList docLib = web.Lists["Process Aids"];
          SPField field = docLib.Fields["Title"];
          field.Title = "Display Name";
          field.Required = true;
          field.Update();
        }
    }
}

Changed code after reading comments

share|improve this answer
2  
If you dont "new" your site, running with elevated privileges changes nothing –  Anders Rask Sep 9 '11 at 5:53
    
Agree with Anders, for RunWithElevatedPriviliges to work you need to create a completely new instance of an SPSite / SPWeb, i.e.using(SPSite curSite = new SPSite(((SPSite)properties.Feature.Parent).Url)) –  Colin Sep 9 '11 at 6:17
    
Thank you. you are right. I changed code –  Vadim Sep 9 '11 at 6:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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