Whats wrong with this code?

Thursday, 15 October 2009 12:30 by tariq
[code]

/// <summary>
/// Gets or sets the site URL.
/// </summary>
/// <value>The site URL.</value>
[WebBrowsable(true),
WebDescription("Url of the site to process reports on"),
WebDisplayName("Site Url"),
Personalizable(PersonalizationScope.Shared)]
public string SiteUrl
{
    get
    {
        if (string.IsNullOrEmpty(siteUrl))
        {
            return SPContext.Current.Web.Url;
        }
        return siteUrl;
    }
    set
    {
        siteUrl = value;
    }
}


[/code]

The above code snippet looks like  a  reasonable WebPart Propperty. In-fact you will find nothing weird with it untill you provision this webpart through a feature.

What happens is the reference to the SPContext…. in the property messes up the WebPart Order on the page and moves the webpart to Order 1 (i.e. first webpart on the zone), no matter what other preference you would have set in your feature.

Interesting eh? Something to keep in mind for future

Tags:   , , ,
Categories:   sharepoint | wss
Actions:   E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Find the NetBios Name of AD

Thursday, 30 July 2009 08:55 by tariq

Its been quite a bit of struggle for me to find an accurate way of finding the netbios name of a domain from AD using System.DirectoryServices.

In case you are in the same jam here how you do it.

  1. Connect to AD using the following ldap url:

    LDAP://CN=Partitions,CN=Configuration,DC=<DomainName>,DC=<local|com>

  2. When querying AD using the Directory Searcher object uses the following filter:

    netbiosname=*

This should give you a record from AD containing the netbios name of the domain as the CN

Explanation

AD stores the the netbios name in the Partitions naming container which is stored inside the configuration naming container.

A more detailed explanation and more samples can be found in the Active Directory Cookbook or its online version

 

Code sample:

// Method call

string netBiosName = GetNetBiosName( LDAP://CN=Partitions,CN=Configuration,DC=<DomainName>,DC=<local|com>,  "<userName"", "<password>");

// Method call

 

// Method Definition

private string GetNetBiosName(
    string ldapUrl,
    string userName,
    string password)
{
    string netbiosName = string.Empty;
    DirectoryEntry dirEntry = new DirectoryEntry(ldapUrl,
            userName, password);

    DirectorySearcher searcher = new DirectorySearcher(dirEntry);
    searcher.Filter = "netbiosname=*";
    searcher.PropertiesToLoad.Add("
cn");

    SearchResultCollection results = searcher.FindAll();
    if (results.Count > 0)
    {
        ResultPropertyValueCollection rpvc = results[0].Properties["CN"];
        netbiosName = rpvc[0].ToString();
    }
    return netbiosName;
}

The MetaBlog API (Creating and Editing Posts)

Monday, 6 July 2009 03:25 by tariq

I was writing an extension for my blog that uses the MetaWeblog API, so here is what I have learnt about it.

It uses XML RPC for communication. So if you are coding in .NET you will need the xml-rpc library from CookComputing.

 

XML-RPC

Firstly an Introduction to XML RPC over here (I would recommend reading the whole series posted there) so that you may get an idea of

1. XML RPC Types

2. Request

3. Response

4. Errors (a.k.a. Faults)

 

Using CookComputing XML-RPC.Net

The following link shows you how to use xml-rpc.net library from cook computing connect to the WordTracker system.

Link

MetaWeblogAPI

I would recommend you reading the MetaWeblogApi spec to have an understanding of what the API is expecting as the payload of the RPC calls.

So getting down to detail

1. Making a Post

The metaWeblog api new post method signature is

metaWeblog.newPost (blogid, username, password, struct, publish) returns string

which translates as the following into csharp decorated with Xml_rpc.net

[XmlRpcMethod("metaWeblog.newPost")]
        string newPost(
            string blogid,
            string username,
            string password,
            XmlRpcStruct rpcstruct,
            bool publish);

Please note the parameter of type XmlRpcStruct, you can use a regular struct instead of this, and XmlRpc.net will translate it into a XmlRpcStruct type.

What is mandatory for this struct to have is a title, description and link. For blogengines that don't support link pass "content" as the value.

Ok so this is fine and dandy, but what about more complex blog engines that support tags, categories etc.

What I have discovered is that a well formed struct for that satisfies this requirement is as follows.

public struct MetaWeblogRpcStruct

{

        public string title;

        public string link;

        public string permaLink;

        public string description;

        public  string[] categories;

        public int mt_allow_comments;

        public string mt_keywords;

        public string wp_slug;

        public string mt_basename;

        public string pubDate;

        public string mt_excerpt;

        public bool publish;

}

so the newpost signature now is

[XmlRpcMethod("metaWeblog.newPost")]
        string newPost(
            string blogid,
            string username,
            string password,
            MetaWeblogRpcStruct mwrpcstruct,
            bool publish);

So coming back to RPC, we need an interface that defines the metaweblog api which we could base a proxy on.

hence

public interface IMetaWeblog: IXmlRpcProxy
{
     [XmlRpcMethod("metaWeblog.newPost")]
     string newPost(
         string blogid,
         string username,
         string password,
         MetaWeblogRpcStruct mwrpcstruct,
         bool publish);

}

So now that that is done, how do you make a post.

First you need to generate the proxy

IMetaWeblog metaWeblog = (IMetaWeblog)XmlRpcProxyGen.Create(typeof(IMetaWeblog));

Then set the endpoint for the RPC call (i.e. metaweblog url of the blogengine)

metaWeblog.Url = "http://localhost/blog/metaweblog.axd";

// Make a post

string ret = metaWeblog.newPost(
                blogId, UserName, Password, struct, struct.publish);

What is returned from the metaweblog api when new post is called? A GUID to the post, you will need this if you are to edit the post later on.

2. Editing a Post

The metaweblog api signature for editing a post is

metaWeblog.editPost (postid, username, password, struct, publish) returns true

which when added to our interface looks like

public interface IMetaWeblog: IXmlRpcProxy

{

     [XmlRpcMethod("metaWeblog.newPost")]

     string newPost(

         string blogid,

         string username,

         string password,

         MetaWeblogRpcStruct mwrpcstruct,

         bool publish);

[XmlRpcMethod("metaWeblog.editPost")]

bool editPost(

    string postId,

    string username,

    string password,

    MetaWeblogStruct content,

    bool publish);

}

The procedure is the same as creating a post except the fist parameter "postId" should be the GUID of the post to update.

Swami's TechWorld - Precompile tip

Saturday, 30 May 2009 12:08 by tariq

Came across this tip at a blog called Swamis Tech World. He shows you how to setup an external tool in Visual Web Developer to have an alternative means of precompiling your website.

Go here for more details.