How to add section to web config programmatic in SharePoint - sharepoint

I have lot of custom entries to be made in a web config in SharePoint.
Is there a way we can do it programatically.
such as i have
this to add under
<PageParserPath>
<PageParserPath VirtualPath="/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true" />
also i have a complete new section to be added called
<connectionstring>
entries here
</connectionstring>
how can i do it programtically any ideas please

Use SPWebConfigModification:
http://spmatt.wordpress.com/2013/05/22/using-spwebconfigmodification-to-update-the-web-config-in-sharepoint-2013/
In your case you would have something along these lines:
var httpRuntimeModification = new SPWebConfigModification();
httpRuntimeModification.Path = "configuration/PageParserPath";
httpRuntimeModification.Name = "myModification";
httpRuntimeModification.Sequence = 0;
httpRuntimeModification.Owner = "MyAppName";
httpRuntimeModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNodes;
httpRuntimeModification.Value = "<PageParserPath VirtualPath='/*' CompilationMode='Always' AllowServerSideScript='true' IncludeSubFolders='true' />";
webApp.WebConfigModifications.Add(httpRuntimeModification);
You probably have to tweak the Xpath as I am not sure where this element lives in the web.config
You should use this in a feature receiver where you can get the reference to your webapplication and you should always remove them on feature deactivation

Related

SharePoint Online - Show/display column in default view

I am adding a site column into a document library default view and want it to be visible/shown when you click onto the list itself. However, I am unsure on how to do this. The code I have so far
// Get the view (this is the default view)
Microsoft.SharePoint.Client.View v = Employeecvlist.GetViewByName("All Documents");
// Load it up
clientContext.Load(v, x => x.ViewFields);
clientContext.ExecuteQuery();
// Get the field I want to add to the view
Microsoft.SharePoint.Client.Field name =
Employeecvlist.Fields.GetByInternalNameOrTitle("Name");
clientContext.Load(name);
clientContext.ExecuteQuery();
// Add this field to the view !! Nothing else in the view object to allow to make it visible by default !!
v.ViewFields.Add(name.InternalName);
// Finally, update the view
v.Update();
If you look at the image file below, I basically want to be able to check the "display" checkbox to true for the above field.
Can someone point me into the right direction?
Thanks
You need to perform clientContext.ExecuteQuery() again to persist the changes. Also, there's no need to do it twice to load your objects, load everything you need and then get it from the server:
//Put following line in the using section
using Microsoft.SharePoint.Client;
//Your code
View v = Employeecvlist.GetViewByName("All Documents");
Field name = Employeecvlist.Fields.GetByInternalNameOrTitle("Name");
clientContext.Load(v, x => x.ViewFields);
clientContext.Load(name);
v.ViewFields.Add(name.InternalName);
v.Update();
clientContext.ExecuteQuery();

Create object in velocity template

I am writing velocity templates for my liferay theme and I am wondering, whether it is possible to create a new object inside the velocity template.
The reason is that in liferay there is no contextTool registered in the context and I really want to be able to inspect the variables that are present in the template at a given time.
There is a cool macro for this, but unfortunately it uses the contexttool.
I'd like to do something like:
#set($contextTool = new ContextTool())
Another solution would be java code that is provided with the liferay theme that is able to add stuff in the template context. But I don't know how this would work either... ;-)
try with
#set($contextTool = $portal.getClass().forName("full.package.ContextTool").newInstance())
EDIT
IF I understood you than this should give you what you want
#set($ve = $serviceLocator.findService("com.liferay.portal.kernel.velocity.VelocityEngine"))
#set($wvc = $ve.getWrappedStandardToolsContext().getWrappedVelocityContext())
#set($cVE = $portal.getClass().forName("org.apache.velocity.app.VelocityEngine"))
#set($cHSREQ = $portal.getClass().forName("javax.servlet.http.HttpServletRequest"))
#set($cHSRES = $portal.getClass().forName("javax.servlet.http.HttpServletResponse"))
#set($cSC = $portal.getClass().forName("javax.servlet.ServletContext"))
#set($cCC = $portal.getClass().forName("org.apache.velocity.tools.view.context.ChainedContext"))
#set($cVEI = $portal.getClass().forName("com.liferay.portal.velocity.VelocityEngineImpl"))
#set($cC = $portal.getClass().forName("org.apache.velocity.context.Context"))
#set($cVEU = $portal.getClass().forName("com.liferay.portal.kernel.velocity.VelocityEngineUtil"))
#set($ve = $cVEU.getMethod("getVelocityEngine").invoke(null))
#set($fVE = $cVEI.getDeclaredField("_velocityEngine"))
$fVE.setAccessible(true)
#set($cc = $cCC.getConstructor($cC, $cVE, $cHSREQ, $cHSRES, $cSC).newInstance($wvc, $fVE.get($ve), $request, $response, $request.getSession().getServletContext()))
#set($contextTool = $portal.getClass().forName("org.apache.velocity.tools.view.tools.ContextTool").newInstance())
$contextTool.init($cc)
After that you can use, for example
$contextTool.getKeys()
If this is not what you need, let me know ...

How to add a Calculated field to AllContentType?

Today, I'm having a problem is after I had created a Calculated field. It seems there is no way to add AllContentTypes. And the DefaultView, maybe I can handle this. And I also saw this method:
spList.Fields.AddFieldAsXml(spFieldUser.SchemaXml, True, SPAddFieldOptions.AddToAllContentTypes);
But in this case, I'm not sure I can use it or not. Because my code is:
//SPField tempSPField = spList.Fields.CreateNewField(createSPColumnObject.ColumnType, createSPColumnObject.ColumnName);//We can not use this code line for creating Calculated (there is no constructor for this)
SPFieldCollection collFields = spList.Fields;
string strSPFieldCalculatedName = collFields.Add(createSPColumnObject.ColumnName, SPFieldType.Calculated, false);
if (createSPColumnObject.IsAddedToDefaultView)
{
SPView spView = spList.DefaultView;
spView.ViewFields.Add(strSPFieldCalculatedName);
spView.Update();
}
SPFieldCalculated spFieldCalculated = null;
//
spFieldCalculated = (SPFieldCalculated)collFields[createSPColumnObject.ColumnName];
spFieldCalculated.ShowInDisplayForm = true;
//spFieldCalculated.ShowInEditForm = true;
spFieldCalculated.ShowInListSettings = true;
//spFieldCalculated.ShowInNewForm = true;
spFieldCalculated.ShowInViewForms = true;
//
spFieldCalculated.Description = createSPColumnObject.ColumnDescription;
spFieldCalculated.Formula = string.Format(#"={0}",createSPColumnObject.CalcFormula);
spFieldCalculated.Update();
//spList.Fields.AddFieldAsXml(spFieldCalculated.SchemaXml, createSPColumnObject.IsAddedToDefaultView, SPAddFieldOptions.AddToAllContentTypes);// also use this code line because we will get an exception with a duplicate column ID.
spFieldCalculated.OutputType = SPFieldType.Text;
spList.Update();
I totally created a Calculated column but how can I add it to allcontent types ? everybody could help me out this ? BTW, to the DefaultView, I did like the above is right ? Could eveybody let me know this ?
I just worry about everybody get misunderstanding ? Or review with missing code. So could everybody please to take a look on my code clearly ? Thanks all.
Many thanks, :)
Standley Nguyen
I'm not sure if i fully understand what you are trying to do however i may be able to shed some light on some parts of what you are trying to do.
When you create your field does it then appear in your site actions -> site settings -> Site columns. If so you have created this correctly. If it doesn't there are hundreds of examples of how to do this if you search google.
Once you have your field create you then need to consider which content types you want to add it to. Once you have these content types you then have to add something called a field link to the Content type.
This isn't my code i have picked it off the web but this should do what you require.
SPContentType ct = web.ContentTypes[contentType];
ct.FieldLinks.Add(new SPFieldLink(field));
ct.Update();
Cheers
Truez

Sharepoint: How to add file to without changing version?

I need to add new version of the file to SPFileCollection without changing version (it is a system update of the content of pdf file)
UpdateOverwriteVersion allows me to change metadata but is there a way to change content (bytes) of the file?
WORKAROUND:
Ok, as Stefan found out, there is no satisfactory solution to this.
I found two workarounds:
1.
(Be aware that turning off the version control could lead to wrong behavior, if there are people working on that list... – Stefan)
oFile = oWeb.GetFile(url);
oFile.Item.ListItems.List.EnableVersioning = false;
oFile.Item.ListItems.List.Update();
oFolder.Files.Add(oFile.Name, aBytes, fileProperties, true);
oFile.Item.ListItems.List.EnableVersioning = true;
oFile.Item.ListItems.List.Update();
2.
oFile = oWeb.GetFile(sAdres);
int iFileVersion = oFile.UIVersion;
oFolder.Files.Add(oFile.Name, aBytes, fileProperties, true);
oFileVersion = oFile.Versions.GetVersionFromID(iFileVersion);
if (null != oFileVersion && !oFileVersion.IsCurrentVersion)
{
oFileVersion.Delete();
}
Why don't you use SystemUpdate(false)
http://msdn.microsoft.com/en-us/library/ms481195.aspx
You can load the SPFile object and then call SPFile.SaveBinary(Stream, SPFileSaveBinaryParameters) method to update it's content.
With the CreateVersion property of the SFileSaveBinaryParameters you can specify whether a new version should be created or not.
[...] this is by design. As far as I know , there is no other solutions on it.
A quote from a moderator at Microsoft TechNet forum.

Sharepoint 2010 - make Title, Description and Keyword fields as required fields in Picture Library using server-object-model

I'm creating a Sharepoint feature, this feature has an event receiver associated to it. In the event receiver, I'm creating a Document Library and Picture Library using server-side object model. I'm also adding new custom columns (around 80) to these newly created document and picture library. Now I want to modify the properties of the Description, Keywords and Title fields that are by default created along with the picture library. I want to make these fields as Required fields. How do I do this? I tried to set SPList.AllowContentTypes = true and try to change the attributes of these fields, but it doesn't work (neither gives an error nor makes these required fields). I also tried to access the content types and try to change the attributes using SPContentType.FieldsLinks["Column_name"].Required and SPContentType.Fields["Column_name"].Required but it gives me an error. Does anyone have any other suggestions?
Here is the answer....
SPContentType ct = mypiclib.ContentTypes["Picture"];
SPFieldLinks titleLink = ct.FieldLinks["Title"];
SPFieldLinks descLink = ct.FieldLinks["comments"]; //internal name of Description
SPFieldLinks keywords = ct.FieldLinks["keywords"];
titlelink.Required = true;
descLink.Required = true;
keywords.Required = true;
ct.Update();
can you tell us the error you got when trying to use the fieldlinks? Because this should work... I have done it like this:
SPContentType ct = web.Lists["*ListName*"].ContentTypes["*ContentTypeName*"];
SPFieldLinkCollection flinks = ct.FieldLinks;
flinks["*ColumnName*"].Required = true;
ct.update();
This should do the trick:
SPWeb yourWeb = ... //assign your web
SPList yourPictureLibrary = ... //assign your picture library
web.AllowUnsafeUpdates = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Title].Required = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Description].Required = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Keywords].Required = true;
yourPictureLibrary.Update();
SPAllowContentTypes isn't settable. You might try setting ContentTypesEnabled instead.
I don't have a 2010 box to test against, but if SPAllowContentTypes returns false I think you're looking at modifying the definition of your picture library in the 14 hive (TEMPLATE\FEATURES\PictureLibrary\PicLib) to get what you're after. Tread lightly there.

Resources