Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Friday, September 4, 2009

SharePoint web.config modification programmatically using c#

Here is the sample code to customize the web.config file for the SharePoint site programmatically
spSite = new SPSite(http://intranet.demo.com);
SPWeb spWeb = spSite.OpenWeb();
SPWebService service = SPWebService.ContentService;
SPWebApplication webApp = new SPSite("http://intranet.demo.com").WebApplication;
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", spSite.WebApplication.Name);
XmlDocument doc = new XmlDocument();
doc.Load(configFilePath);
AppSettingsSection appSettings = config.AppSettings;
if (appSettings.ElementInformation.IsPresent)
{
XmlNode nodekey = doc.SelectSingleNode("configuration/appSettings/add[@key='key']");
if(nodekey == null)
{
SPWebConfigModification myModification = new SPWebConfigModification();
myModification.Path = "configuration/appSettings";
myModification.Name ="add[@key='userName']";
myModification.Sequence = 0;
myModification.Owner = "User Name";
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
myModification.Value =
";
service.WebConfigModifications.Add(myModification);
}
}
/*Call Update and ApplyWebConfigModifications to save changes*/
service.Update();
service.ApplyWebConfigModifications();
As you can see, it is pretty simple and if you want to apply to the site spWeb.Site.WebApplication.WebConfigModifications.Add(MyModification)
If you want to apply to the whole farm
webApp.Farm.Services.GetValue().ApplyWebConfigModifications();
wenApp.Update();

Thursday, August 27, 2009

Create SharePoint Document Library programmatically using C#

We can create our own custom document library programmatically, here is the code shown below

SPSite site = SPContext.Current.Site as SPSite;
SPWeb web = site.RootWeb as SPWeb;
try
{ //Create a new library as the permanent library
Guid customListID = web.Lists.Add("CUSTOM DOCUMENT LIBRARY", "CUSTOM DOCUMENT LIBRARY", SPListTemplateType.DocumentLibrary);
web.Update();
}

Thursday, August 20, 2009

SharePoint Cannot complete this action error

When i am trying to count the CheckedOut items from SharePoint Document Library using c# code in visual studio as shown below

SPSite mysite = new SPSite("http://servername:portnumber");
SPWeb myWeb = mysite.OpenWeb();
myWeb.AllowUnsafeUpdates = true;
SPList Document = myWeb.Lists["Shared Documents"];
SPQuery query = new SPQuery();
query.Query ="<Where><IsNotNull><FieldRef Name='CheckoutUser' /></IsNotNull></Where>";
SPListItemCollection checkedOutItems= Document.GetItems(query);
Response.Write(checkedOutItems.Count);

I got the following error:
System.Runtime.InteropServices.COMException: Cannot complete this action

Resolution:

We need add the tag "<identity impersonate='true'/>" tag in the web.config file of the project in the visual studio.
This is because the ASP.NET doesnt allows you to read the outside documents i.e the documents(folders) are not in ASP.NET Application.So we need to keep impersonate = true in web.config file.

Hopes this helps you....

Monday, August 17, 2009

c# programmatically check the "<appSettings>" tag in web.config file of sharepoint sites

To check programmatically weather the appSettings tag is present in the web.config file of SharePoint sites, here is the code

using (SPSite site = new SPSite("http://servername:portnumber"))
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", mySite.WebApplication.Name);
AppSettingsSection appSettings = config.AppSettings;
bool isAppSettingsTagPresent = appSettings.ElementInformation.IsPresent;
}

hope this helps you very much...

Friday, August 7, 2009

Programmatically get the sharePoint List Url in C#

To get the Share Point list "url" based on list "Guid", here is the code shown below

SPSite Site = SPContext.Current.Site;
SPWeb web = Site.OpenWeb();
Guid listid = new Guid("{xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}");
web.AllowUnsafeUpdates = true;
SPList List = web.Lists[listid];
string url = SPContext.Current.Site.Url + "/" + List.RootFolder.Url;
Response.Write(url);
web.AllUnSafeUpdates = false;
web.Dispose();

Hopes this helps you a lot.

Thursday, August 6, 2009

Set "everyone" Permission to Folder using C# in Asp.Net

Here is the simple C# code to set "everyone" permissions to the folder .

string redirectionFolder = @"D:\test\subtest\";

FileSystemAccessRule everyOne = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl,AccessControlType.Allow);
DirectorySecurity dirSecurity = new DirectorySecurity(redirectionFolder, AccessControlSections.Group);
dirSecurity.AddAccessRule(everyOne);
Directory.SetAccessControl(redirectionFolder, dirSecurity);

Note:
If you get the error "Attempted to perform an unauthorized operation"

add the tag (<identity impersonate="true"/>) in your web.config file.