Solved

SharePoint Hidden Column

Posted on 2011-02-16
6
842 Views
Last Modified: 2012-05-11
Hi,

I'm trying to hide a column in SharePoint 2007 in a document library.  The column needs to be hidden so that users cannot update the field, but it needs to be visible in the default view.  That is the easy part and I've successfully done that.

However, my problem arises when I try to edit a document via C# code using the SharePoint dlls.  My code is as follows:

foreach (SPFile document in docLibrary.Files)
{
     string name = document.Properties["MyColumn"];
}

when the column is hidden, it is null.  However, when it's optional, it returns the default value.

How can I hide this column from users but still be able to update it in code?  Thank you!
0
Comment
Question by:88brad
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:Rafael I
ID: 34912077
How are you hiding the column?
0
 

Author Comment

by:88brad
ID: 34913145
Going into each content type the column is added for and selecting hidden instead of optional/required.
0
 
LVL 6

Accepted Solution

by:
Rafael I earned 250 total points
ID: 34916923
That sounds really complicated.  Why don't you try using JavaScript to hide the column(s) you don't want to be shown instead?

First you need to "edit" the page you want, in this case NewForm.aspx by adding "PageView=Shared&ToolPaneView=2" to the URL (no quotes, of course)

Then follow the directions here: http://www.sharepointkings.com/2009/10/hiding-field-in-sharepoint-through.html

Basically, you edit your NewForm.aspx and add a Content Editor Web Part, and use a JavaScript function to hide the field you want.  You should be able to see it in your code since the column is just like all the other columns now.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 3

Assisted Solution

by:Nomoho
Nomoho earned 100 total points
ID: 35025766
Hello,

About your code:
foreach (SPFile document in docLibrary.Files)
{
     string name = document.Properties["MyColumn"];
}

You want to display the content of your column or edit it?
To display it I use:
document.Item["MyColumn"] instead, I am not sure of what is containing document.Properties["MyColumn"]

To edit it,
document.Item["MyColumn"] = value;
document.Item.SystemUpdate(false); // (Or Update() if you want to raise event handlers)

Hope this may help you.
0
 

Assisted Solution

by:88brad
88brad earned 0 total points
ID: 35127967
Thanks for all your help, document.Item["MyColumn"] ended up not working either - the property didn't exist (even though it had a value on the page.

I ended up adding a property and saved it like this:

     document.Item.Properties.add("MyColumn", "VALUE");
     document.Item.Update();

After this, then I could access the property.  Not ideal, but I would just add "Default Value" if it didn't exist.  It was still hidden after this but thanks for everyone's help.
0
 

Author Closing Comment

by:88brad
ID: 35170721
Rawsteak's solution was where I was going if my solution didn't work.  Nomoho had the right idea but was missing some details.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This very simple solution applies to a narrow cross-section of the "needs to close" variety. In this case, the full message in Event Viewer was in applog, Event ID 1000: Faulting application iexplore.exe, version 8.0.6001.18702, faulting module …
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
Viewers will learn the different options available in the Backstage view in Excel 2013.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question