locked
How to make a spfieldtext readonly programtically ? RRS feed

  • Question

  • Hi

    I have created a survey in sharepoint 2007 programtically and trying to add spfield of type Text.I need to make that field  Read only.

    I tried this;

    SPFieldText sptxt = new SPFieldText(objSurvey.Fields,SPFieldType.Text.ToString(), fieldname);
    sptxt.MaxLength = Convert.ToInt32(txtCharNos.Text);
    sptxt.DefaultValue = txtdefault.Text;
    sptxt.ReadOnlyField = true;
    objSurvey.Fields.Add(sptxt);
    objSurvey.Update();

    But ReadOnlyField property doesn't work while others do.

    Please help me to make that particular field read only.


    Celesta Joseph


    Friday, October 12, 2012 6:57 AM

Answers

All replies

  • Hi,

    Try this:

    SPFieldText sptxt = new SPFieldText(objSurvey.Fields,SPFieldType.Text.ToString(), fieldname);
    sptxt.MaxLength = Convert.ToInt32(txtCharNos.Text);
    sptxt.DefaultValue = txtdefault.Text;
    objSurvey.Fields.Add(sptxt);
    objSurvey.Update();
    
    SPField field = list.Fields.GetFieldByInternalName(sptxt); 
    field.ReadOnlyField = true;
    field.Update();

    Check below blog for your ref: http://www.etechplanet.com/blog/adding-a-columnfield-in-sharepoint-list-programmatically-using-c.aspx

    Hope it could help


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Friday, October 12, 2012 9:31 AM
    Moderator
  • Hi Hemendra,

    Thanks for your reply.

    Can you please explain me this line of code?

    SPField field = list.Fields.GetFieldByInternalName(sptxt); 

    What must be the parameter is to be used in  GetFieldByInternalName()?

    Thanks in Advance


    Celesta Joseph

    Friday, October 12, 2012 10:51 AM
  • You need to pass the field name instead of sptxt so SPField can read the name of your column and update it as per our code.

    Check this MSDN for your ref:

    http://msdn.microsoft.com/en-us/library/ff521580.aspx


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"


    Friday, October 12, 2012 10:56 AM
    Moderator

  • I passed fieldname as the parameter.But the readonlyfield property does not fire.

    What could be the problem?Any other remedies?

    Thanks in Advance


    Celesta Joseph


    Friday, October 12, 2012 11:28 AM
  • Hi,

    I think i forgot to add list.Update() at the end of code. Try to add this line and execute your code.

    If above solution does not work then try below code:

    SPFieldText sptxt = new SPFieldText(objSurvey.Fields,SPFieldType.Text.ToString(), fieldname);
    sptxt.MaxLength = Convert.ToInt32(txtCharNos.Text);
    sptxt.DefaultValue = txtdefault.Text;
    objSurvey.Fields.Add(sptxt);
    objSurvey.Update();
    
    
    foreach (SPField field in objSurvey.Fields)
    {
    if (field.Title == "Put your column name here..it is case sensitive so be careful")
    {
    field.ReadOnlyField = true;
    field.Update();
    break;
    }
    }
    objSurvey.Update(); 

    Let us know your result now


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Friday, October 12, 2012 11:53 AM
    Moderator
  • It is not working.I tried this but doesn't work.

    field.ReadOnlyField = true


    Celesta Joseph

    Monday, October 15, 2012 4:51 AM
  • Hi,

    May be some permission issue. Are you getting any error? Have you tried to debug you code and see in which line it is creating problem.

    Also use

    //at the beginning
    web.AllowUnsafeUpdates = true;
    
    foreach (SPField field in objSurvey.Fields)
    {
    if (field.Title == "Put your column name here..it is case sensitive so be careful")
    {
    field.ReadOnlyField = true;
    field.Update();
    break;
    }
    }
    objSurvey.Update(); 
    //at the last
    web.AllowUnsafeUpdates = false;
    
    
    

    You can also add below line of if there is any account issue.

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        // implementation details omitted
    });

    Let us know your result


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Monday, October 15, 2012 5:15 AM
    Moderator
  • Thank you for your support.But The solution  that you posted does not work.By Read only  field I mean no user can edit the default value contained in that field.While debugging there is no error.But the result is not reflecting in the survey list.When I executed "ReadOnlyField=true",the field completely disappeared. Is there any other way?I found SetCustomProperty() method related to spfield.Do you have any idea?

    Thanks In  Advance


    Celesta Joseph



    Monday, October 15, 2012 5:53 AM
  • Hi,

    This is default behavior for list because survey list does not have any content type so you do have few option to apply on list. You can try with javascript/jquery on newform.aspx page of survey list where you can get your control ID and make textbox readonly.

    Check below thread for jquery:

    http://social.msdn.microsoft.com/Forums/en/sharepointcustomization/thread/b0366f45-62d7-4be0-b706-eed1d8e02722

    Hope it could help


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Tuesday, October 16, 2012 5:08 AM
    Moderator
  • Hi,

    Have you resolved you problem? If yes then provide your solution so other can take benefit of that and if above post help you to resolve your problem then mark it as answer.


    Hemendra: "Yesterday is just a memory,Tomorrow we may never see"

    Whenever you see a reply and if you think is helpful, click "Alternate TextVote As Helpful"! And whenever you see a reply being an answer to the question of the thread, click "Alternate TextMark As Answer

    Friday, November 16, 2012 10:08 AM
    Moderator