SharePoint: LINQ query to get child content types

17 July 2008

I had been putting off using / learning linq for a little while, but am now finally writing some .net 3.5 code for sharepoint where there are some clear uses for it. I am finding it really intuitive.

Anyway, below is one of my first real-world linq queries - it is a simple query to get all of the descendant content types from a given content type (note that the parent content type is also included):

SPContentTypeId parentId = new SPContentTypeId("parentContentTypeId");

using (SPSite mySiteCollect= new SPSite("http://url/to/my/spweb"))
{
    using (SPWeb website = siteCol.OpenWeb())
    {
        IEnumerable<SPContentType> descendantContentTypes =
           from SPContentType childCt in myspweb.AvailableContentTypes
           where childCt.Id.IsChildOf(parentId)
           select childCt;

        // Do something here with descendant content types
    }
}



post a comment / view comments   (currently 0 comments)

SharePoint: 403 Error Using Embedded Resources

16 July 2008

I encountered an issue yesterday using embedded resources in a .NET 2.0 WebPart consumed by SharePoint. The dll used by the webpart contained an embedded js and css file that was accessed by this.Page.ClientScript.GetWebResourceUrl (note dll was deployed to 80\bin).

Whenever a page containing the webpart was accessed first by a non-admin user after an app pool recycle, a 403 error was encountered. This error went away to all users once an admin user had viewed the webpart. This error implied to me that the appropriate safecontrol assembly setting in the web.config was not correct, however this was not the case.

I have not yet found a proper solution to this problem, however the workaround I am currently using  is to call GetWebResourceUrl via SPSecurity.RunWithElevatedPrivileges. I will update this post if and when I find the actual cause / solution to this problem.

Related Link:

http://www.katriendg.com/aboutdotnet/2007-6-webresources-webpart-403-forbidden.aspx



post a comment / view comments   (currently 0 comments)

Passed MCPD Exam 70-547

16 July 2008

I have just passed the "Designing and Developing Web-Based Applications by Using the Microsoft .NET Framework" exam. This means that I am now a Microsoft Certified Professional Developer.

MCPD(rgb)_504



post a comment / view comments   (currently 0 comments)

Updating webpart properties from outside the tool pane

12 July 2008

Today I created a webpart that contained a form. I wanted the input values from the form to be persisted in the same way as normal webpart properties that are set through the toolpane. To achieve this, I added appropriate attributes to the property I wanted to persist (notably WebBrowsable(false), as I did not want the property to be edited through the UI). I also ensured that the SetPersonalizationDirty() method was called after the property was set - this is crucial to ensure that the value is persisted. The code below illustrates this:

public class SavedSearch : System.Web.UI.WebControls.WebParts.WebPart
{
     private string _savedFreeTextSearch;
     private TextBox _inputBox;
     private Button _inputButton;
     private Label _label;

     [WebBrowsable(false),
     Personalizable(PersonalizationScope.Shared),
     DefaultValue("")]
     public string SavedFreeTextSearch
     {
         get { return _savedFreeTextSearch; }
         set { _savedFreeTextSearch = value; }
     }

     protected override void CreateChildControls()
     {
         _label = new Label();
         _label.Text = this.SavedFreeTextSearch;
         this.Controls.Add(_label);

         _inputBox = new TextBox();
         this.Controls.Add(_inputBox);

         _inputButton = new Button();
         _inputButton.Click += new EventHandler(_inputButton_Click);
         this.Controls.Add(_inputButton);

         base.CreateChildControls();
     }

     private void _inputButton_Click(object sender, EventArgs e)
     {
         this.SavedFreeTextSearch = _inputBox.Text;
         _label.Text = this.SavedFreeTextSearch;
         this.SetPersonalizationDirty();
     }
}



post a comment / view comments   (currently 0 comments)

SharePoint Content Types: Hidden Attribute

07 July 2008

Just a quick note - the hidden attribute on a content type seems to have little effect on how a content type can be used. The content type can still be used on document libraries, however the only side effect is that the content type does not appear on the new button drop down (even though "Visible on New Button" is set to true). The content type still appears when you upload a document.

Hmm, I'm just thinking as I write - maybe the hidden attribute stops a content type from being used in Office directly (so it is just hidden to Office)? If true, this may form a workaround to the Document Information Panel (DIP) problem (where custom field types are not rendered correctly in the DIP) by stopping content types that use custom field types from being accessed in Office.

This is just speculation at the moment - it will be interesting to see how the DIP behaves when a document is already set to use a hidden content type, I will test tomorrow morning...

Related link:

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

EDIT:

I have just tested in office, and hidden content types are available both when creating new docs and when updating docs that already use a hidden content type. This poses the question, what is the hidden attribute for if it only hides the content type from the new button?



post a comment / view comments   (currently 0 comments)