Thursday, October 9, 2008

Renaming a MOSS Server

Here is the step to rename the moss server that is already configure it.

  • Change each alternate access mapping for your MOSS/WSS deployment in Central Administration:
    • Open Central Administration, "Operations" Tab, "Alternate access mappings" link
    • Modify each mapping item to reflect your newly chosen server name
  • Use stsadm.exe to invoke the "renameserver" command option:
    • Open a command prompt window
    • cd "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN"
    • stsadm -o renameserver -newservername -oldservername
    • Reboot the server NOW.
    • After reboot, open command prompt
      • cd C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
      • stsadm -o updatefarmcredentials -userlogin -password
      • iisreset /noforce
    • Check all application pool identities in IIS, update where the old machine name is still there.
    • If you already have a search index drop this, and rebuild it

    The use of SPContext

    When building custom web parts for Windows SharePoint Services V3. You can use the SPContext object to get for instance the site or web from the current context. But that's not all you can get from SPContext. You can also get the listitem and the webfeatures.

    In WSS V2 you would use SPControl.GetContextSite() or SPControl.GetContextWeb() but that's a lot less flexible and it's slower as well.

    A couple of different uses of SPContext are shown below:

    *************************************************************************

    SPList currentList = SPContext.Current.List;

    SPWeb currentSite = SPContext.Current.Web;

    SPSite currentSiteCollection = SPContext.Current.Site;

    SPWebApplication currentWebApplication = SPContext.Current.Site.WebApplication;

    *************************************************************************

    SPListItem item = (SPListItem)SPContext.Current.Item;

    *************************************************************************

    SPWeb site = SPContext.Current.Site.OpenWeb(guid);

    SPUser user = SPContext.Current.Web.CurrentUser;

    *************************************************************************

    SPSiteDataQuery siteQuery = new SPSiteDataQuery();

    siteQuery.Query = "" +

    "100";

    siteQuery.ViewFields = "";

    DataTable queryResults = SPContext.Current.Web.GetSiteData(siteQuery);

    queryResults.TableName = "queryTable";

    queryResults.WriteXml("C:\\queryTable.xml");

    The last example makes use of the SPSiteDataQuery object. In WSS V3 there are two different query objects. Besides SPSiteDataQuery object there is also the SPQuery object.
    The difference between the two is that SPQuery can only be used for queries within a single folder in a single list. SPSiteDataQuery can be used for cross lists and even cross site queries.

    SharePoint Web Services

    The WSS Web Services are part of the Microsoft.SharePoint.SoapServer namespace. The web services include methods to accessing and customizing SharePoint site content such as lists, site data, forms, meetings, document workspaces, and permissions. The table below shows the WSS web services, including a description of what they can be used for and the reference.

    MOSS 2007 also exposes a series of web services. The MOSS web services allow you to use the Business Data Catalog, document management, Enterprise Search, Excel Services, InfoPath Forms Services, and Web content management (WCM). The table below shows most of the web services, there use and the reference you need to use them.

    If you want to use one of the SharePoint web serrvices in your visual studio project you can simply add a Web Reference using the path of the SharePoint site for which you want to use the Web Service and add the web service reference to it. Your reference will then look something like this http://moss/_vti_bin/[webservicereference].asmx.

    Uploading a File to a SharePoint Site from a Local Folder

    I have read the this tutorial and it is good for add document in the list from local folder.
    http://msdn.microsoft.com/en-us/library/ms868615.aspx

    Thursday, October 2, 2008

    Active Directory Change Password

    protected void Button1_Click(object sender, EventArgs e)

    {
    if (this.txtNewPassword.Text.Length >= 0)
    {

    if (this.txtNewPassword.Text.Equals(this.txtConfirmPassword.Text))
    {
    DirectoryEntry entry;
    char[] sep ={ '\\' };
    string[] strArray = this.Context.User.Identity.Name.Split(sep);
    if (this.isLocalAccount())
    {

    try

    {
    // Connect to Active Directory and get the DirectoryEntry object.
    // Note, ADPath is an Active Directory path pointing to a user.

    entry = new DirectoryEntry("WinNT://" + strArray[0] + "/" + strArray[1], this.Context.User.Identity.Name, this.txtOldPassword.Text, AuthenticationTypes.Secure);
    }

    catch (Exception exception1)

    {

    lblMsg.Text = exception1.Message.ToString();

    return;

    }

    try

    {

    object objectValue = RuntimeHelpers.GetObjectValue(entry.Invoke("ChangePassword", new object[] { this.txtOldPassword.Text, this.txtNewPassword.Text }));

    }

    catch (Exception exception5)

    {



    lblMsg.Text = exception5.Message.ToString();

    return;

    }

    }

    else

    {

    try

    {

    string[] propertiesToLoad = new string[] { "sAMAccountName", "cn" };

    DirectorySearcher searcher = new DirectorySearcher("(sAMAccountName=" + strArray[1] + ")", propertiesToLoad);

    searcher.SearchRoot.Username = this.Context.User.Identity.Name;

    searcher.SearchRoot.Password = this.txtOldPassword.Text;



    // You would have created this which searches AD for the specified user

    // and returns its DirectoryEntry object or path. See here.

    SearchResult result = searcher.FindOne();

    if (result == null)

    {



    lblMsg.Text = "The User was not found in the Active Directory";

    }

    entry = new DirectoryEntry(result.Path, this.Context.User.Identity.Name, this.txtOldPassword.Text);

    // entry.RefreshCache();





    DirectorySearcher search = new DirectorySearcher(entry);

    search.Filter = "sAMAccountName=" + strArray[1].ToString(); ;

    SearchResult result1 = search.FindOne();

    DirectoryEntry user = result1.GetDirectoryEntry();

    string NewPassword = this.txtNewPassword.Text;

    user.Invoke("SetPassword", new object[] { NewPassword });

    lblMsg.Text = "Password has been changed successfully.";





    }

    catch (Exception exception6)

    {



    lblMsg.Text = exception6.Message.ToString();

    return;

    }



    }



    // lblMsg.Text = lblMsg.Text + this.SuccessMessage;

    }

    else

    {



    // lblMsg.Text = lblMsg.Text + this.ErrorMessagePasswordMatch;

    }

    }

    else

    {



    lblMsg.Text = "Password doesn’t match requirements.";

    }





    }

    Add new element in the web.config file using sharepoint

    If you can add new element the web.config file using sharepoint administration SPWebConfigModification Object that have open web.config file and find the appropriate element tag in the web.config file and add new element in the web.config.

    You can use this code in the feature actived.


    try

    {

    SPWebApplication app = null;

    SPWeb web = null;

    SPSiteCollection site = properties.Feature.Parent as SPSiteCollection;

    if (site == null)

    {

    web = properties.Feature.Parent as SPWeb;

    if (web != null)

    app = web.Site.WebApplication;

    }

    else

    app = site.WebApplication;

    SPWebConfigModification modification = new SPWebConfigModification();

    modification.Name = "add [@Assembly='System.DirectoryServices'] [@Version='2.0.0.0'][@Culture='neutral'][@PublicKeyToken='B03F5F7F11D50A3A']";

    modification.Path = "configuration/system.web/compilation/assemblies";

    modification.Value = @"DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A'/> ";

    modification.Owner = Assembly.GetExecutingAssembly().FullName;

    modification.Sequence = 0;

    modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;

    app.WebConfigModifications.Add(modification);

    SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

    }

    catch (Exception ex)

    {


    }


    The example code could be changed easily to remove the configuration change, as in the following example:

    app.WebConfigModifications.Remove(modification);

    Add new feature in personal menu

    Here is a feature.xml and elements.xml that can add new menu in the person menu item.

    <Feature Id="50E6274F-668C-4f5f-AC85-7DE468957230" Title="My site" Description="My site"
    Version="1.0.0.0" Scope="Web" xmlns="http://schemas.microsoft.com/sharepoint/"
    ReceiverAssembly="trushar.trushar, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b44e792107d2d835"
    ReceiverClass="trushar.trushar.trushar"
    >

    <ElementManifests>
    <ElementManifest Location="elements.xml"/>
    </ElementManifests>
    </Feature>


    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction Id="PersonalActionsMenu_MyMenu" GroupId="PersonalActions"
    Location="Microsoft.SharePoint.StandardMenu" Sequence="101"
    Title="My Menu"
    Description="My Menu"
    ImageUrl="_layouts/images/menuprofile.gif" >
    <UrlAction Url="~site/_layouts/test.aspx"/>
    </CustomAction>
    </Elements>