Thursday, February 17, 2011

Folder with Title Column

When we create a Folder in a SharePoint(2007/2010) Document Library, in "Edit Properties" Page of the Folder we can only change the Name of it. Another point is that the Name of the Folder uses as a part of its URL. 
Sometimes there is a need to keep the Folder path URL short and also has a kind of description or user friendly name for your Folder. Out of the box, SharePoint default Folder Content Type doesn't support this for some reasons the Title column is hidden and there isn't the possibility of change it. To have such a Folder with Title column you can complete these steps:
  • Navigate to Site Settings > Site Content Types
  • Create a new Content Type and choose "Folder Content Types" as its Parent Content Type
  • Click on Title column of the new created content type and for its Column Settings choose
  • Go to your Document Library and add new created Content Type to it.

Monday, February 7, 2011

Filling fields using javascript and web services

Some days ago there was a request for filling a look up field in New Item page automatically according to the current user information. One of the ways of doing this, is coding for custom field but the other quick and easy way is using JavaScript and web services

1- Adding a field with default value of [Me] to hold the user name of current user
2- Adding a content Editor webpart in NewForm.aspx page of List and put the code below inside its source editor:

<script type="text/javascript" language="javascript">

    var siteCompleteURL = document.URL;
    componentList = siteCompleteURL.split('//');
    siteURL = componentList[1].split('/');

    var curUserLastName = new Array();
    var curUserID = new Array();
    var baseURL = componentList[0] + "//" + siteURL[0] + "/";
    var siteURL = componentList[0] + "//" + siteURL[0] + "/";

    //Get The Value Of Hidden UserName Field
    var curUser = document.getElementById('USERNAMEID').value;


    //Hide The UserName Field and its Label
    document.getElementById('
USERNAMEID').style.visibility='hidden';
    var nobrTags = new Array();
    nobrTags=document.getElementsByTagName('nobr');
    for (i = 0; i < nobrTags.length; i++) {
    if (nobrTags(i).innerHTML=='UserName')
      nobrTags(i).innerHTML='';
     }

    //Get The LookUp Filed Object
    var expOwnerCID='LOOKUPFIELDID';

    //GUID Of The List We Want To Query
    var listGUID = 'd6e7ebe5-79b1-4e92-b50a-adf6d4c79c6e';

    //Where Statement of Web service Request
    var whereStatement = "<Eq><FieldRef Name=\"_x0646__x0627__x0645__x0020__x06\" /><Value Type=\"User\">"+curUser+"</Value></Eq>";
   
    function GetRootUrl() {
        return siteURL;
    }

    function getNodeValue(obj, tag) {
        return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
    }

    function QueryListEx(listGuid, where, rowLimit, extractRows) {
        var a = new ActiveXObject("Microsoft.XMLHTTP");
        if (a == null) return null;
        a.Open("POST", GetRootUrl() + "_vti_bin/dspsts.asmx", false);
        a.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        a.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/dsp/queryRequest");

        var d = '<?xml version=\"1.0\" encoding=\"utf-8\"?>'
    + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
    + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
    + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope\/\">"
    + " <soap:Header xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope\/\">"
    + " <dsp:versions xmlns:dsp=\"http://schemas.microsoft.com/sharepoint/dsp\">"
    + " <dsp:version>1.0</dsp:version>"
    + " </dsp:versions>"
    + " <dsp:request xmlns:dsp=\"http://schemas.microsoft.com/sharepoint/dsp\""
    + " service=\"DspSts\" document=\"content\" method=\"query\">"
    + " </dsp:request>"
    + " </soap:Header>"
    + "<soap:Body>"
    + "<queryRequest "
    + " xmlns=\"http://schemas.microsoft.com/sharepoint/dsp\">"
    + " <dsQuery select=\"/list[@id='" + listGuid + "']\""
    + " resultContent=\"dataOnly\""
    + " columnMapping=\"attribute\" resultRoot=\"Rows\" resultRow=\"Row\">"
    + " <Query RowLimit=\"" + rowLimit + "\">"
    + " <Where>" + where + "</Where>"
    + " </Query>"
    + " </dsQuery>"
    + " </queryRequest>"
    + "</soap:Body>"
    + "</soap:Envelope>";

        a.Send(d);

        if (a.status != 200)
            return null;

        var xmlDoc = a.responseXML;
        var responseElement = xmlDoc.getElementsByTagName("Row");
        var temp = '';        
        for (i = 0; i < responseElement.length; i++) {
            //Store Title Column Value
            curUserLastName[i] = responseElement[i].getAttribute('Title');

            var expOwner = curUserLastName[i];
           
            //To Execute This Code At The End Of The Page
            Sys.Application.add_load(function () {
                document.getElementById(
                getSubControlID(expOwnerCID, g_EntityEditorUpLevelId)).innerHTML = (expOwner);
                onKeyUpRw(expOwnerCID);
            });
                    }
    }
    QueryListEx(listGUID, whereStatement, 1, true);
</script>

A good source for JavaScript API that help me to complete this job: