Thursday, May 5, 2011

How to Roll-up the cost at the summary task level

Sub Test()
Dim cst1 As Integer
Dim cst As Integer
Dim t, t1 As Task
Dim OutlineLevel As Integer
cst1 = 0
OutlineLevel = 1

While OutlineLevel < 15
For Each t In ActiveProject.Tasks
If Not (t Is Nothing) Then
If (t.OutlineLevel = OutlineLevel And t.Summary = True) Then
For Each t1 In t.OutlineChildren
cst = t1.Cost1
cst1 = cst + cst1
Next t1
t.Cost1 = cst1
cst1 = 0
End If
End If
Next t
OutlineLevel = OutlineLevel + 1
Wend
End Sub

Monday, October 11, 2010

Apply Validation using Javascript in Sharepoint


1. Add the CEWP on the EditForm or Newform.aspx
2. Paste Below script in that and change the types and respective Control names
3. To pass correct parameter in the "TagName" function follow
TAGNAME: HTML element that is being rendered ("SELECT", "INPUT"...)
IDENTIFIER: SharePoint field type identifier ("TextField", "DropDownChoice"...)
FIELD NAME: Display name of the field (e.g. "Status", "Customer Name"...)



function PreSaveAction()
{
var Email = TagName("INPUT","TextField","Customer Email Id");
var result=checkEmail(Email.value);
if(result== "0")
{
alert("Please enter proper Email address");
return false; // Cancel the item save process
}
return true;

}
function TagName(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}
function checkEmail(inputvalue){
var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
if(pattern.test(inputvalue)){
return 1;
}else{
return 0;
}
}

Sunday, October 10, 2010

Create Full WBS Structure in MSP 2007

Recently got a requirement to create the Full WBS structure with the Task Names in the MSP 2007.

Following is the Macro to achieve the same:

Sub Test()
Dim str As String
Dim t As Task
Dim OutlineLevel As Integer
OutlineLevel = 1
While OutlineLevel < 15
For Each t In ActiveProject.Tasks
If Not (t Is Nothing) Then
If t.OutlineLevel = OutlineLevel Then
t.Text10 = t.OutlineParent.Text10 & "." & t.Name
End If
End If
Next t
OutlineLevel = OutlineLevel + 1
Wend
End Sub

And we are done, It has helped when i was suppose to give data back to my client where they needed the Full path so they can map it with their actual data.
Hope this will help someone.. J

Thursday, October 7, 2010

And Navratri is starting...

Cant wait to see guruji @ Ashram....

Reading List Items using JQuery

Recently got an Requirement where i was suppose to send a request to customer to fill the Questionnaire for the feedback...


  • Below form will be initiated by the Project Manager to have the customer feedback.
    Mail will reach to the Client with the hyper link of the List's Edit item, An Item will be added to the Customer Feedback form and also Mail will be Shoot the client
    Then Client will see the below shown form

    And He will fill the Questionnaire and later on the CFB Score will get sync to the Project Server.
  • So how can we achieve such scenario, here we go open the list's EditForm.aspx and place the content editor webpart and place them above the form control.
  • And here we Go >>
    First Give the reference to the JQuery Folder as usual..
    <script language="javascript" type="text/javascript" src="/_layouts/jquery/jquery-1.3.2.min.js"></script>


    To Fetch the ID's Value from the Query String....


    function queryString(parameter) {
    var loc = location.search.substring(1, location.search.length);
    var param_value = false;
    var params = loc.split("&");
    for (i=0; i<params.length;i++) {
    param_name = params[i].substring(0,params[i].indexOf('='));
    if (param_name == parameter) {
    param_value = params[i].substring(params[i].indexOf('=')+1)
    }
    }
    if (param_value) { return param_value; }
    else { return false; //Here determine return if no parameter is found }
    }
    var param=queryString('ID');


    This will Fetch the data from Web Services
  • Now based on the Id fetched from the query string we have to traverse through the data which we have got using webservices used in below code
    <script language="javascript">
  • $(document).ready(function(){
    var soapEnv_CFF =
    "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
    <soapenv:Body> \
    <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
    <listName>Customer Feedback Form</listName> \
    <viewFields> \
    <ViewFields> \
    <FieldRef Name='Id' /> \
    <FieldRef Name='Title' /> \
    </ViewFields> \
    </viewFields> \
    </GetListItems> \
    </soapenv:Body> \
    </soapenv:Envelope>";
    $.ajax({
    url: "../../_vti_bin/lists.asmx",
    type: "POST",
    dataType: "xml",
    data: soapEnv_CFF,
    complete: processResult_CFF,
    contentType: "text/xml; charset=\"utf-8\""
    });
    });
  • This Function will fetch data which we require to populate based on the IDs and append their values in the variable which we are going to use in the html representation


  • function processResult_CFF(xData, status) {
    $(xData.responseXML).find("z\\:row").each(function() {
    if($(this).attr("ows_ID")==param) {
    param=$(this).attr("ows_Title");
    var liHtml = "<td>Sow No./Workorder No :</td><td>" + $(this).attr("ows_ID") + "</td><td></td><td></td>" ;
    $("#tasksUL").append(liHtml);
    }
    });
    }
    </script>
  • This is the HTML tags where we can use the appended html content
    <table style="background-color:Silver" border="0" cellspacing="2" cellpadding="1" width="100%">
    <tr style="background-color:#d6ebff" id="tasksUL"></tr>
    </table>
    </html>

So without using the sharepoint designer we can actually show the data from other list using jQuery and content editor webpart. really easy, isnt it..!!? :)

Wednesday, October 6, 2010

Hide SharePoint List Items from New Form or Edit Form without SP Designer

So, what if you want to hide fields from the SharePoint list without tempering the list in SP Designer. Here is the Script.... J

  1. Open the NewForm.aspx or EditForm.aspx
  2. "&PageView=Shared&ToolPaneView=2" Add this text in the url and you can open the page in edit mode
  3. Add the CEWP and Click on the source Editor

And put the below script in it...


<script language="javascript" type="text/javascript">
_spBodyOnLoadFunctionNames.push("hideFields");
function findacontrol(FieldName)
{
var arr = document.getElementsByTagName("!");
// get all comments
for (var i=0;i < arr.length; i++ )
{
// now match the field name
if (arr[i].innerHTML.indexOf(FieldName) > 0)
{ return arr[i]; }
}
}
function hideFields()
{
var control = findacontrol("CustomerID");
control.parentNode.parentNode.style.display="none";
}
</script>

See, the script is pretty simple, you can push your own Function on the page load and then it will call hideFields() Function which will find out the Control name from the page's HTML and set that display to "none", its done.

Add list Items using JQuery


Recently i got an requirement where i was suppose to add list items using JavaScript as we did not have access to the server side code, and we were not able to write code on the server.
So Here we go...

  1. Add JQuery to your Layouts Folder and give reference to the CEWP

    <script type="text/javascript" src="/_layouts/jquery/jquery-1.3.2.min.js"></script>

  2. Add the Script which will initialize the Web Service and Create the XML

    $(document).ready(function() {

    $("#newTaskButton").click(function() {

    CreateNewItem($("#newTaskTitle").val());

    });

    });


    function CreateNewItem(title) {

    // The CAML to create a new item and set the Title field.

    var batch ="<Batch OnError=\"Continue\"> \

    <Method ID=\"1\" Cmd=\"New\"> \

    <Field Name=\"Title\">" + title + "</Field> \

    </Method> \

    </Batch>";

    // The SOAP Envelope

    var soapEnv =

    "<?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:Body> \

    <UpdateListItems xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\"> \

    <listName>Tasks</listName> \

    <updates> \

    " + batch + "</updates> \

    </UpdateListItems> \

    </soap:Body> \

    </soap:Envelope>";


    // Build the URL of the Lists.asmx web service.

    // This is done by stripping the last two parts (/doclib/page) of the URL.

    var hrefParts = window.location.href.split('/');

    var wsURL = "";

    for (i = 0; i < (hrefParts.length - 2); i++) {

    if (i > 0)

    wsURL += "/";

    wsURL += hrefParts[i];

    }

    wsURL += "/Pilot/_vti_bin/lists.asmx";

    // Make the call by posting the SOAP Envelope.

    $.ajax({

    url: wsURL,

    beforeSend: function(xhr) {

    xhr.setRequestHeader("SOAPAction",

    "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");

    },

    type: "POST",

    dataType: "xml",

    data: soapEnv,

    complete: processResult,

    contentType: "text/xml; charset=utf-8"

    });

    }

    function processResult(xData, status) {

    // Process the result.

    $("#responseStatus").text(status);

    $("#responseXML").text(xData.responseXML.xml);

    }


  3. Create an HTML Control


    <input id="newTaskTitle" type="text" />

    <input id="newTaskButton" type="button" value="Create Task" />

    <h5>

    Response:

    <label id="responseStatus" visibility="false">N/A </label>

    </h5>

  4. Now All put together and you are done.....!!!!