Friday, April 24, 2009

Updating Task Data using PSI

Hello Friends, 
This is my first blog releated to Project Server Interface(PSI).
In this I will show how we can update Task Data and Assignment Data Using PSI.
I have taken Windows Project for this example.

1] First of all create a Windows Project.
2] Add Project Web Service(Here I have named Project Web Service as WebSvcProject) using Web Reference.
3] I have taken one windows form and added one button to it. On that button click I have called function UpdateTaskData("Test")  and passed project name. I have used this project name to update its Task data. 

Following is the function which will update Task Fields.
In this function I have updated Task Name field.

Add reference of Microsoft.Office.Project.Server.Library
Imports System.Data
Imports PSLibrary = Microsoft.Office.Project.Server.Library
Imports System.Web.Services.Protocols
Public Class PSI
Public Shared PSI_PATH As String = System.Configuration.ConfigurationSettings.AppSettings("PSI_PATH").ToString()
Public Shared Sub UpdateTaskData(ByVal projnm As String)
        Dim projectDs As New WebSvcProject.ProjectDataSet
        Dim projuid As Guid
        Dim sessionUid As Guid
        Dim jobUId As Guid = Guid.NewGuid
        Dim taskDT As New WebSvcProject.ProjectDataSet.TaskDataTable
        Dim projectSvc As New WebSvcProject.Project()
        Dim i As Integer = 0
        Try
           '' Below is the project service url.
            projectSvc.Url = PSI_PATH + "Project.asmx"
           '' Pass your credentials accordingly.
            projectSvc.Credentials = New System.Net.NetworkCredential("username", "pwd")
'' In projectDs we get Project list using ReadProjectList() function
            projectDs = projectSvc.ReadProjectList()
''In For loop I have read project details for the Project name that is passed to the function
            For Each row As WebSvcProject.ProjectDataSet.ProjectRow In projectDs.Project.Rows
                If row.PROJ_NAME = projnm Then
                    projuid = row.PROJ_UID
                    projectDs = projectSvc.ReadProject(row.PROJ_UID ,               WebSvcProject.DataStoreEnum.PublishedStore)
                    Exit For
                End If
            Next

             sessionUid = Guid.NewGuid
''Here Project is checked out so that we can update the Task Name field.
            projectSvc.CheckOutProject(projuid, sessionUid, "Descr")
               For Each row As WebSvcProject.ProjectDataSet.TaskRow In projectDs.Task.Rows
                    If Not row.TASK_ID = 0 Then
                         row.TASK_NAME = "NewTsk" & i
                         i += 1
                   End If
            Next
            projectSvc.QueueUpdateProject(jobUId, sessionUid, projectDs, False)
            projectSvc.QueuePublish(jobUId, projuid, True, "")
        Catch ex As SoapException
            Dim errmsg As String = ""
            Dim err As PSLibrary.PSClientError = New PSLibrary.PSClientError(ex)
            Dim errors As PSLibrary.PSErrorInfo() = err.GetAllErrors()
            For i As Integer = 0 To errors.Length - 1
                errmsg = "\n" + ex.Message.ToString() + "\r\n"
                errmsg += errors(i).ErrId.ToString() + "\n"
                For j As Integer = 0 To errors(i).ErrorAttributes.Length - 1
                    errmsg += "\r\n\t" + errors(i).ErrorAttributeNames()(j) + ": " +                errors(i).ErrorAttributes(j)
                Next
            Next
            MessageBox.Show(errmsg)
        Finally
            projectSvc.QueueCheckInProject(Guid.NewGuid, projuid, True, sessionUid, "")
        End Try
    End Sub
End Class

I hope this will be helpful for you.