Showing posts with label SharePoint Programmatically. Show all posts
Showing posts with label SharePoint Programmatically. Show all posts

Thursday, March 10, 2011

Using SharePoint Feature Receiver create Virtual Directory in IIS

As per my previous post you create virtual directory from command line.
Here another way to create virtual directory in IIS site.
Everything is possible programmatically.
I created one SharePoint Feature to create virtual directory in IIS’s SharePoint site.




string _serverName = string.Empty;
string _userName = string.Empty;
string _password = string.Empty;
string _webSiteName = string.Empty;
string _physicalPath = string.Empty;

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPWeb tempweb;
SPSite tempsite;

if (properties.Feature.Parent is SPWeb)
{
tempweb = properties.Feature.Parent as SPWeb;
tempsite = tempweb.Site;
}
else
{
tempsite = properties.Feature.Parent as SPSite;
tempweb = tempsite.RootWeb;
}

GetWebServiceParamFromConfig(tempweb.Url);
if (!_serverName.Equals(string.Empty) && !_userName.Equals(string.Empty) && !_password.Equals(string.Empty) && !_webSiteName.Equals(string.Empty) && !_physicalPath.Equals(string.Empty))
{
string webSiteId = GetIISWebsiteID(_serverName, _webSiteName);

String strRootSubPath = "/W3SVC/" + webSiteId + "/Root";

String strSchema = "IIsWebVirtualDir";

DirectoryEntry objRootDirectoryEntry = new DirectoryEntry("IIS://" + _serverName + strRootSubPath);
objRootDirectoryEntry.Username = _userName;
objRootDirectoryEntry.Password = _password;
objRootDirectoryEntry.RefreshCache();
DirectoryEntry objNewVDir = objRootDirectoryEntry.Children.Add("CustomWebService", strSchema);
objNewVDir.Properties["Path"].Insert(0, _physicalPath);
objNewVDir.CommitChanges();
deRoot.CommitChanges();

objNewVDir.Invoke("AppCreate", true);

objNewVDir.CommitChanges();
objRootDirectoryEntry.CommitChanges();
objNewVDir.Close();
deRoot.Close();
}
else
{
Utility.ErrorLog("During WebService Vir. Dir. Create in IIS - Param. Empty", System.Diagnostics.EventLogEntryType.Error, "CreateWebServiceVirDir - FeatureActivated()");
}
}
catch (Exception ex)
{
Utility.ErrorLog("Error during WebService Vir. Dir. Create in IIS - " + ex.Message, System.Diagnostics.EventLogEntryType.Error, "CreateWebServiceVirDir - FeatureActivated()");
}
}

public void GetWebServiceParamFromConfig(string webUrl)
{
DataSet ds = GetDataFromList(webUrl);
DataTable dtWebServiceParam = ds.Tables["WebService"];

if (dtWebServiceParam != null && dtWebServiceParam.Rows.Count > 0)
{
_serverName = dtWebServiceParam.Rows[0]["ServerName"].ToString();
_userName = dtWebServiceParam.Rows[0]["Username"].ToString();
_password = dtWebServiceParam.Rows[0]["Password"].ToString();
_webSiteName = dtWebServiceParam.Rows[0]["WebSiteName"].ToString();
_physicalPath = dtWebServiceParam.Rows[0]["PhysicalPath"].ToString();
}
}

public string GetIISWebsiteID(String server, String websiteName)
{
DirectoryEntry w3svc = new DirectoryEntry("IIS://" + server + "/w3svc");
String webId = "";
foreach (DirectoryEntry de in w3svc.Children)
{
if (de.SchemaClassName == "IIsWebServer" && de.Properties["ServerComment"][0].ToString() == websiteName)
{
webId = de.Name;
}
}
return webId;
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
SPWeb tempweb;
SPSite tempsite;

if (properties.Feature.Parent is SPWeb)
{
tempweb = properties.Feature.Parent as SPWeb;
tempsite = tempweb.Site;
}
else
{
tempsite = properties.Feature.Parent as SPSite;
tempweb = tempsite.RootWeb;
}

GetWebServiceParamFromConfig(tempweb.Url);

if (!_serverName.Equals(string.Empty) && !_userName.Equals(string.Empty) && !_password.Equals(string.Empty) && !_webSiteName.Equals(string.Empty) && !_physicalPath.Equals(string.Empty))
{
string webSiteId = GetIISWebsiteID(_serverName, _webSiteName);
String strRootSubPath = "/W3SVC/" + webSiteId + "/Root";
String strSchema = "IIsWebVirtualDir";
DirectoryEntry objRootDE = new DirectoryEntry("IIS://" + _serverName + strRootSubPath);
objRootDE.Username = _userName;
objRootDE.Password = _password;
objRootDE.RefreshCache();
DirectoryEntry deChild = new DirectoryEntry("IIS://" + _serverName + strRootSubPath + "/CustomWebService");
objRootDE.Children.Remove(deChild);
}
else
{
Utility.ErrorLog("During WebService Vir. Dir. Delete from IIS - Param. Empty", System.Diagnostics.EventLogEntryType.Error, "CreateWebServiceVirDir - FeatureDeactivating()");
}
}
catch (Exception ex)
{
Utility.ErrorLog("Error during WebService Vir. Dir. Remove from IIS - " + ex.Message, System.Diagnostics.EventLogEntryType.Error, "CreateWebServiceVirDir - FeatureDeactivating()");
}

}


Happy Coding !!!

Wednesday, January 19, 2011

Save multiple choice values in SharePoint ListItem

You will find that your checkbox list is populated with the selected value.
Save selected checkboxes in the list item as below



SPFieldMultiChoiceValue multiValue = new SPFieldMultiChoiceValue();
foreach (ListItem item in multiValueInputFormCheckBoxList.Items)
{
if (item.Selected) multiValue.Add(item.Value);
}
item["multivalued choice field name"]= multiValue;


Happy Coding !!!

Display multiple choice value of saved Listitem

Show those values which was stored in the SharePoint ListItem.
For display selected value use previous post’s “InputFormCheckBoxList”
Select listitem for display choice field’s multiple value as below



if (item["multivalued choice field name"] != null)

{

SPFieldMultiChoiceValue multiValue = new SPFieldMultiChoiceValue(item["multivalued choice field name"]);

for (int i=0; i<= multiValue.Count; i++) multiValueInputFormCheckBoxList.Items.FindByText(multiValue[i]).Selected = true; }


Happy Coding !!!

Bind SharePoint multiple choice field in custom form

Bind SharePoint multiple choice field in custom form.

You can bind sharepoint multiple choice value in sharepoint check box list control for display Choice field values.
Add SharePoint “InputFormCheckBoxList” and bind as below code.


ASPX Design Code:




Use below code to populate the choice column values in above control.
Server Side CS Code:

// -------------- Populate CheckBox List
SPField multiValueCheckBoxField = objSPList.Fields["multivalued choice field name"];
SPFieldMultiChoice multiValueCheckBoxChoicesField = (SPFieldMultiChoice) multiValueCheckBoxField ;

// for each loop for values add in checkbox list
foreach (string choiceValue in multiValueCheckBoxChoicesField.Choices)
{
this.multiValueInputFormCheckBoxList.Items.Add(new ListItem(choiceValue, choiceValue));
}


Happy Coding!!!