in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Ethan's blog

October 2007 - Posts

  • Specify What Forms a Field Shows Up On

    I am often asked about field level security in SharePoint. Well, SharePoint does not have field level security but it does have the ability to specify which forms a field will show up on. For example, we can have a field show up on the New form and all view forms but not on the edit form. This is by no means security, but it does allow for certain  scenarios, like after a value is entered it cannot be changed. So, this post will show you how to change those settings on fields.

    There are 2 different ways to set these properties. The first is through the list definition. When we create a list definition we can certain attributes on the Field element tag as follows.

    • ShowInEditForm - Determines if this field shows up in the Edit form for a specific field.
    • ShowInNewForm - Determines if this field show up in the New form for adding a new item to this list.
    • ShowInVersionHistory - Determines if this field shows up in the version details of a list item.
    • ShowInViewForms - Determines if this field shows up in the any View forms for a specific item.

    This post is going to show you the second way, in code, to set these properties. Through the SharePoint API you can also set these properties of a specific field. These are optional value fields, meaning that they can be null. So the first time you look at these, they might have a null value in them.

    The first thing we need to do is get a reference to the list that contains the field that we want to set these values on.

    // Create the site that contains our list
    SPSite oSite = new SPSite(strSiteUrl);

    // Open the web object
    SPWeb oWeb = spSite.OpenWeb();

    // Get the list that contains the field we want to change the settings on
    SPList oList = oWeb.Lists["Contacts"];

    Now that we have our list, we need to get the field that we want to set the settings on.

    // Get the field that we want to change the settings on
    SPField oField = oList.Fields["Title"];

    Now we can set the properties of our field according to our requirements. There are a couple more properties available in the API than there are in the List Definition schema.

    // Set the field properties
    oField.ShowInDisplayForm = true;     // Determines in this field shows on the display item form.
    oField.ShowInEditForm = false;        // Determines if this field shows on the edit item form.
    oField.ShowInListSettings = true;     // Determines if this field shows on the list settings page.
    oField.ShowInNewForm = true;        // Determines if this field shows on the new item form.
    oField.ShowInVersionHistory = true;// Determines if this field shows on the version history pages.
    oField.ShowInViewForms = true;    // Determines in this field shows on the all view forms pages.

    Now all we have to do is call the update method on the field object to actually update the settings and save the changes back to the database.

    // Update the field
    oField.Update();

    Continue reading if you want to see how to accomplish the same tasks in SWAT (SharePoint Work Acceleration Toolkit). You can find SWAT here http://www.idevfactory.com

    Set Field Settings

    In the Farm Tree window, rick click on the site that you want to set the web parts properties for and click Show Site Details. This will load all the details for that site.

    In the Site Details section, select the Site Lists tab.

    In the Site Lists tab, right click on the list that has the field that you want to set the properties on and click Show List Details.

    On the List Details window, right click on the field that you want to set the properties for and click Show Field Settings.

    On the Field Details window, select the specific settings that you want to change and click Update.

    Enjoy!!!

  • Working With Web Parts Through Code

    This post will show you how to work with web parts through code. Often times there is a need to work with web parts through the API. You can import, export, add remove as well as change settings of web parts.

    Importing and Adding Web Parts.

    The first thing we need to get is out site and web object.

    SPSite oSite = new SPSite("http://yoursiteurl");
    SPWeb oWeb = oSite.OpenWeb();

    The next thing we need to get is the page (file) that we want to import the web part on. For this example we will be working with the default.aspx page on a team site.

    SPFile oFile = oWeb.GetFIle("default.aspx");

    After we have a file object we can get the limited web part manager. The limited web part manager is what we use to do all of our web part operations.

    Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager oWebPartManager = oFile.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

    Now we need to create a StringReader class. The constructor we are using takes a string as a parameter. This is simply the xml file of the web part that we want to import. (The web part .dwp or .webpart file).

    System.IO.StringReader oReader = new StringReader(<<web part xml file (.dwp or .webpart file string contents>>);

    Now we need to create a XmlReader and pass in our string reader.

    XmlTextReader oXmlReader = new XmlTextReader(oReader);

    We need to create a string for our output message.

    string strErrorMessage = "";

    No back to our web part manager, we need to import the web part. We simply call the ImportWebPart method on our limited web part manager object. The first parameter is the xml reader we created before and the second parameter is the output message passed back to us as a out parameter. This method will return a WebPart object.

    System.Web.UI.WebControls.WebParts.WebPart oWebPart = spWebPartManager.ImportWebPart(xmlReader, out strErrorMessage);

    No that we have the web part, we can set specific settings that we want to control for our web part. For example, if we don't want the close x to show up on the top right of the web part we could set the AllowHide property of the web part.

    oWebPart.AllowClose = false;

    No that we have actually imported the web part we need to actually add it to the site. We do this by calling the AddWebPart method on the limited web part manager. The first parameter is the web part that we imported in the previous step. The second parameter is the  zone id (as a string) that you want to add the web part to and the third parameter is the zone order (index).

    oWebPartManager.AddWebPart(oWebPart, "left", 1);

    Full code listing.

    // Get the site
    SPSite oSite = new SPSite("http://yoursiteurl");

    // Get the web we are going to import and add the web part to
    SPWeb oWeb = oSite.OpenWeb();

    // Get the file (page) that we want to add the web part to
    SPFile oFile = oWeb.GetFIle("default.aspx");

    // Get the limited web part manager
    Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager oWebPartManager = oFile.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

    // Create the string reader for the web part .dwp or .webpart file.
    System.IO.StringReader oReader = new StringReader(<<web part xml file (.dwp or .webpart file string contents>>);

    // Create the xml text reader to read the string into
    XmlTextReader oXmlReader = new XmlTextReader(oReader);

    // Create the error message that will get passed back
    string strErrorMessage = "";

    // Import the web part
    System.Web.UI.WebControls.WebParts.WebPart oWebPart = spWebPartManager.ImportWebPart(xmlReader, out strErrorMessage);

    // Now actually add the web part to the page
    oWebPartManager.AddWebPart(oWebPart, "left", 1);

    NOTE: You can also remove and export web parts through the limited web part manager.

    Continue reading if you want to see how to accomplish the same tasks in SWAT (SharePoint Work Acceleration Toolkit). You can find SWAT here http://www.idevfactory.com

    Set Web Part Properties

    In the Farm Tree window, rick click on the site that you want to set the web parts properties for and click Show Site Details. This will load all the details for that site.

    In the Site Details section, select the Site Pages tab.

    In the Site Pages tab, right click on the page you want to look at the web parts of and click Show Page Web Parts.

    On the Page Web Parts window, right click on the web part that you want to set the settings on and click Web Part Properties.

    On the Web Part Properties window, select the specific properties you wish to set and click Update.

    NOTE: You can also close and open a web part from this screen as well.

    NOTE: Under the Wizards menu of SWAT, there a 3 different wizards dealing with web parts. Add Web Parts, Replicate Web Parts and Delete Web Parts. These wizards allow you to work with many web parts on many different sites all in one action.

    Enjoy!!!


Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts