Top Menu

Friday, June 4, 2010

SharePoint 2007 Web Service: Get Items

Use lists.asmx to get the items through a desktop application

Below is the C# function to call the lists.asmx MOSS Web Service to get list items from a list/document library.
Add reference to lists.asmx to your project and use the following code:


private void GetItems(string strListName)
{

try
{
    #region Connect to URL

    this.m_objWSSListService.Url = this.cmbURL.Text.Trim('/') + "/_vti_bin/lists.asmx";

    string sDomain = string.Empty;
    string sUserName = this.txtLogin.Text.Trim();
    if (sUserName.IndexOf("\\") > 0)
    {
        sDomain = sUserName.Split("\\".ToCharArray())[0];
        sUserName = sUserName.Split("\\".ToCharArray())[1];
    }

    this.m_objWSSListService.Credentials = new System.Net.NetworkCredential(sUserName, this.txtPassword.Text.Trim(), sDomain);

    #endregion

    string sListName = strListName;
    string SourceDocumentURL = "";
    string strTempLocation = "C:\\Temp\\";
    string strFileName = "";

    System.Xml.XmlNode xnListSchema = m_objWSSListService.GetList(sListName);

    //get the Data from the List
    System.Xml.XmlDocument xdListData = new System.Xml.XmlDocument();
    System.Xml.XmlNode xnQuery = xdListData.CreateElement("Query");
    System.Xml.XmlNode xnViewFields = xdListData.CreateElement("ViewFields");
    System.Xml.XmlNode xnQueryOptions = xdListData.CreateElement("QueryOptions");

    System.Xml.XmlNode xnListData = m_objWSSListService.GetListItems(sListName, null, xnQuery, xnViewFields, null, xnQueryOptions);


    if (!Directory.Exists(strTempLocation))
    {
        Directory.CreateDirectory(strTempLocation);
    }

    foreach (XmlNode outerNode in xnListData.ChildNodes)
    {
        if (outerNode.NodeType.Equals(System.Xml.XmlNodeType.Element))
        {
            foreach (XmlNode node in outerNode.ChildNodes)
            {
                if (node.NodeType.Equals(System.Xml.XmlNodeType.Element))
                {
                    MessageBox.Show(node.Attributes.GetNamedItem("ows_Title").InnerText);
                }
            }
        }
    }

}
catch (System.Exception ex)
{

    this.WriteLog(ex.ToString());
}

}

Wednesday, June 2, 2010

SharePoint 2007 Web Service: Create Folder in Document Library

Use lists.asmx to create folder in a document library through a desktop application

Below is the C# function to call the lists.asmx MOSS Web Service to create folders in a document library.
Add reference to lists.asmx to your project and use the following code:



private void CreateFolder(string strPath, string strListName)
{

try
{
#region Connect to URL

this.m_objWSSListService.Url = this.cmbURL.Text.Trim('/') + "/_vti_bin/lists.asmx";

string sDomain = string.Empty;
string sUserName = this.txtLogin.Text.Trim();
if (sUserName.IndexOf("\\") > 0)
{
    sDomain = sUserName.Split("\\".ToCharArray())[0];
    sUserName = sUserName.Split("\\".ToCharArray())[1];
}

this.m_objWSSListService.Credentials = new System.Net.NetworkCredential(sUserName, this.txtPassword.Text.Trim(), sDomain);

#endregion

System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement Batch = xmlDoc.CreateElement("Batch");

Batch.SetAttribute("OnError", "Continue");
Batch.SetAttribute("ListVersion", "0");
string strBatch = "<method id="1" cmd="New">";
strBatch = strBatch + "<field name="ID">New</field>";
strBatch = strBatch + "<field name="FSObjType">1</field>";
strBatch = strBatch + "<field name="FileRef">" + strPath + "</field></method>";

Batch.InnerXml = strBatch;
XmlNode ndReturn = this.m_objWSSListService.UpdateListItems(strListName, Batch);
if (ndReturn.FirstChild.FirstChild.InnerText != "0x00000000")
{
    this.WriteLog(ndReturn.InnerText);
}

this.WriteLog("Create Operation - List:" + strListName + " - Folder: " + strPath);


}
catch (System.Exception ex)
{
this.WriteLog(ex.ToString());
}

}

SharePoint 2007 Web Service: Download Documents

Use lists.asmx to download the documents from a document library through a desktop application

Below is the C# function to call the lists.asmx MOSS Web Service to download the documents from a document library.
Add reference to lists.asmx to your project and use the following code:


private void DownloadDocumnts(string strListName)
{

try
{
    #region Connect to URL

    this.m_objWSSListService.Url = this.cmbURL.Text.Trim('/') + "/_vti_bin/lists.asmx";

    string sDomain = string.Empty;
    string sUserName = this.txtLogin.Text.Trim();
    if (sUserName.IndexOf("\\") > 0)
    {
        sDomain = sUserName.Split("\\".ToCharArray())[0];
        sUserName = sUserName.Split("\\".ToCharArray())[1];
    }

    this.m_objWSSListService.Credentials = new System.Net.NetworkCredential(sUserName, this.txtPassword.Text.Trim(), sDomain);

    #endregion

    string sListName = strListName;
    string SourceDocumentURL = "";
    string strTempLocation = "C:\\Temp\\";
    string strFileName = "";

    System.Xml.XmlNode xnListSchema = m_objWSSListService.GetList(sListName);

    //get the Data from the List
    System.Xml.XmlDocument xdListData = new System.Xml.XmlDocument();
    System.Xml.XmlNode xnQuery = xdListData.CreateElement("Query");
    System.Xml.XmlNode xnViewFields = xdListData.CreateElement("ViewFields");
    System.Xml.XmlNode xnQueryOptions = xdListData.CreateElement("QueryOptions");

    System.Xml.XmlNode xnListData = m_objWSSListService.GetListItems(sListName, null, xnQuery, xnViewFields, null, xnQueryOptions);


    if (!Directory.Exists(strTempLocation))
    {
        Directory.CreateDirectory(strTempLocation);
    }

    foreach (XmlNode outerNode in xnListData.ChildNodes)
    {
        if (outerNode.NodeType.Equals(System.Xml.XmlNodeType.Element))
        {
            foreach (XmlNode node in outerNode.ChildNodes)
            {
                if (node.NodeType.Equals(System.Xml.XmlNodeType.Element))
                {
                    XmlNode FileNameNode = node.Attributes.GetNamedItem("ows_EncodedAbsUrl");
                    SourceDocumentURL = FileNameNode.InnerText;
                    strFileName = SourceDocumentURL.Substring(SourceDocumentURL.LastIndexOf('/') + 1).Replace("%20", " ");

                    /*
                     * ?noredirect=true 
                     * will not open the file in default program (e.g. XSN for InfoPath), instead it will download the file
                     * 
                     * */

                    System.Net.WebClient objWebClient = new System.Net.WebClient();
                    objWebClient.Credentials = CredentialCache.DefaultCredentials;
                    objWebClient.DownloadFile(SourceDocumentURL + "?noredirect=true" ,strTempLocation + strFileName);

                    this.WriteLog("File Downloaded: " + SourceDocumentURL);
                }
            }
        }
    }

}
catch (System.Exception ex)
{

    this.WriteLog(ex.ToString());
}

}

Tuesday, June 1, 2010

SharePoint 2007 Web Service: Create User Group

Use UserGroup.asmx to create User Group through a desktop application

Below is the C# function to call the UserGroup.asmx MOSS Web Service to create Uer Groups.
Add reference to UserGroup.asmx to your project and use the following code:


private void CreateUserGroup(string strGroupName, string strDescription)
{
try
{

    #region Connect to UserGroup.asmx

    this.objUserGroupService.Url = this.cmbURL.Text.Trim('/') + "/_vti_bin/UserGroup.asmx";

    string sDomain = string.Empty;
    string sUserName = this.txtLogin.Text.Trim();
    if (sUserName.IndexOf("\\") > 0)
    {
        sDomain = sUserName.Split("\\".ToCharArray())[0];
        sUserName = sUserName.Split("\\".ToCharArray())[1];
    }

    this.objUserGroupService.Credentials = new System.Net.NetworkCredential(sUserName, this.txtPassword.Text.Trim(), sDomain);

    #endregion

    this.objUserGroupService.AddGroup(strGroupName, this.txtLogin.Text.Trim(), "user", this.txtLogin.Text.Trim(), strDescription);
    this.WriteLog("Created Group: " + strGroupName);


}
catch (System.Exception ex)
{
    
    this.WriteLog(ex.ToString());
}
}

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...